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 Mockito方法内的mock对象_Java_Unit Testing_Mocking_Mockito - Fatal编程技术网

Java Mockito方法内的mock对象

Java Mockito方法内的mock对象,java,unit-testing,mocking,mockito,Java,Unit Testing,Mocking,Mockito,我正在编写一个测试,用于验证从SOAP服务接收不同响应时类的行为。 我使用Jaxb,因此我的响应包含jaxbelents,对于其中的许多,我需要编写一个模拟,如下所示: JAXBElement<String> mock1 = mock(JAXBElement.class); when(mock1.getValue()).thenReturn("a StringValue"); when(result.getSomeStringValue()).thenReturn(mock1);

我正在编写一个测试,用于验证从SOAP服务接收不同响应时类的行为。 我使用Jaxb,因此我的响应包含
jaxbelents
,对于其中的许多,我需要编写一个模拟,如下所示:

JAXBElement<String> mock1 = mock(JAXBElement.class);
when(mock1.getValue()).thenReturn("a StringValue");
when(result.getSomeStringValue()).thenReturn(mock1);

JAXBElement<Integer> mock2 = mock(JAXBElement.class);

when(mock2.getValue()).thenReturn(new Integer(2));
when(result.getSomeIntValue()).thenReturn(mock2);
... <continue>
并定义一种方法:

private <T> JAXBElement<T> mockWithValue(Class<JAXBElement> jaxbElementClass, T value) {
    JAXBElement<T> mock = mock(jaxbElementClass);
    when(mock.getValue()).thenReturn(value);
    return mock;
}
其中第126行是
mockWithValue
方法的第一次调用


所以问题是:有没有一种方法可以重用相同的代码来创建许多具有类似行为的模拟?

在创建响应时不应该进行模拟

让我解释一下,在这段代码中

private <T> JAXBElement<T> mockWithValue(Class<JAXBElement> jaxbElementClass, T value) {
    JAXBElement<T> mock = mock(jaxbElementClass);
    when(mock.getValue()).thenReturn(value);
    return mock;
}

试试这个,它会起作用。

当涉及到一些额外的泛型时,最好使用
doReturn()…When()
语法:

    doReturn(mockWithValue(JAXBElement.class, "a StringValue"))
        .when(result).getSomeStringValue();

    doReturn(mockWithValue(JAXBElement.class, 2))
        .when(result).getSomeIntegerValue();

private-JAXBElement-mockWithValue(类jaxbElementClass,T-value){
JAXBElement mock=mock(jaxbElementClass);
doReturn(value).when(mock).getValue();
返回模拟;
}

因为JaxB生成的类是简单的DTO,没有任何业务行为,所以不应该模拟它们……因为构建它们需要其他对象(Qname名称、类declaredType、类作用域、T值),并且它失去了可读性。无论如何,我可以做到,但我遇到了这个例外,我想理解它。我仍然不明白为什么,但是的,它是有效的!非常感谢。另请参见提示#3:如果完成了“thenReturn”指令,则在“thenReturn”指令之前,您正在删除另一个模拟的行为
private <T> JAXBElement<T> mockWithValue(Class<JAXBElement> jaxbElementClass, T value) {
    JAXBElement<T> mock = mock(jaxbElementClass);
    when(mock.getValue()).thenReturn(value);
    return mock;
}
String stringResponse=mockWithValue(JAXBElement.class, "a StringValue");
when(result.getSomeStringValue()).thenReturn(stringResponse);
    doReturn(mockWithValue(JAXBElement.class, "a StringValue"))
        .when(result).getSomeStringValue();

    doReturn(mockWithValue(JAXBElement.class, 2))
        .when(result).getSomeIntegerValue();
    private <T> JAXBElement<T> mockWithValue(Class<JAXBElement> jaxbElementClass, T value) {
        JAXBElement<T> mock = mock(jaxbElementClass);
        doReturn(value).when(mock).getValue();
        return mock;
    }