Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对于不同的情况,如何从模拟方法返回多个值? 新模型(){ @嘲弄 布尔getValue(){ 返回true; } };_Java_Unit Testing_Junit_Jmockit - Fatal编程技术网

Java 对于不同的情况,如何从模拟方法返回多个值? 新模型(){ @嘲弄 布尔getValue(){ 返回true; } };

Java 对于不同的情况,如何从模拟方法返回多个值? 新模型(){ @嘲弄 布尔getValue(){ 返回true; } };,java,unit-testing,junit,jmockit,Java,Unit Testing,Junit,Jmockit,我想根据测试用例从getValue()返回不同的值。如何做到这一点?要在不同的测试中具有来自同一模拟类的不同行为,需要在每个单独的测试中指定所需的行为。例如,在这种情况下: new MockUp<SomeClass>() { @Mock boolean getValue() { return true; } }; 公共类MyTest { @使用实体模型()测试公共无效测试 { 新建模型(){@M

我想根据测试用例从getValue()返回不同的值。如何做到这一点?

要在不同的测试中具有来自同一模拟类的不同行为,需要在每个单独的测试中指定所需的行为。例如,在这种情况下:

    new MockUp<SomeClass>() {
        @Mock
        boolean getValue() {
            return true;
        }
    };
公共类MyTest
{
@使用实体模型()测试公共无效测试
{
新建模型(){@Mock boolean getValue(){return true;}};
//调用测试下的代码,然后调用SomeClass#getValue()。
}
@使用实体模型()测试其他测试
{
新建模型(){@Mock boolean getValue(){return false;}};
//调用测试下的代码,然后调用SomeClass#getValue()。
}
@测试公共void testusingexpections(@NonStrict final SomeClass mock)
{
新的期望(){{mock.getValue();result=true;}};
//调用测试下的代码,然后调用SomeClass#getValue()。
}
@使用Expections测试其他测试(
@非严格期末考试(模拟)
{
//实际上不需要,因为布尔值的默认值为“false”:
新期望(){{mock.getValue();result=false;}};
//调用测试下的代码,然后调用SomeClass#getValue()。
}
}

当然,您可以创建可重用的
MockUp
Expectations
子类,但它们也会在每个需要特定行为的测试中实例化。

为每个测试用例创建新的MockUp或Expectations是否有问题?您是否考虑过使用结果委托?我不知道如何在同一个类上创建同一方法的多个模型。它将只执行第一个模型。你能用一个例子来详细说明你的问题吗?getValue是我的测试单元中的一个依赖项-它是包含被测试单元的类中的一个公共方法。我有两个测试用例——一个需要依赖项返回true,另一个需要依赖项返回false。这是我以前尝试过的,但在第二个用例中它从未执行过模型。我想这是因为我忘了在测试课的另一个地方有一个对整个班级的模拟。
public class MyTest
{
    @Test public void testUsingAMockUp()
    {
        new MockUp<SomeClass>() { @Mock boolean getValue() { return true; } };

        // Call code under test which then calls SomeClass#getValue().
    }

    @Test public void anotherTestUsingAMockUp()
    {
        new MockUp<SomeClass>() { @Mock boolean getValue() { return false; } };

        // Call code under test which then calls SomeClass#getValue().
    }

    @Test public void testUsingExpectations(@NonStrict final SomeClass mock)
    {
        new Expectations() {{ mock.getValue(); result = true; }};

        // Call code under test which then calls SomeClass#getValue().
    }

    @Test public void anotherTestUsingExpectations(
        @NonStrict final SomeClass mock)
    {
        // Not really needed because 'false' is the default for a boolean:
        new Expectations() {{ mock.getValue(); result = false; }};

        // Call code under test which then calls SomeClass#getValue().
    }
}