我正在编写一个测试,以验证我的类在接收来自SOAP服务的不同响应时的行为。 我使用JAXB,所以我的回应包含JaxbElements
,对于他们来说,我需要写一个假,这样的:Mockito模拟方法内的对象
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>
什么,我想这样做,是refactorize这段代码的方式:
when(result.getSomeStringValue())
.thenReturn(mockWithValue(JAXBElement.class, "a StringValue");
when(result.getSomeIntValue())
.thenReturn(mockWithValue(JAXBElement.class, 2));
,并定义一个方法:
private <T> JAXBElement<T> mockWithValue(Class<JAXBElement> jaxbElementClass, T value) {
JAXBElement<T> mock = mock(jaxbElementClass);
when(mock.getValue()).thenReturn(value);
return mock;
}
当我执行的代码重构一切正常了。 不幸的是,当我重构之后执行所述的代码,我收到此错误:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.mypackage.ResultConverterTest.shouldConvertASuccessfulResponseWithAllTheElements(ResultConverterTest.java:126)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
其中线126是mockWithValue
方法的第一次调用。
所以问题是:有没有一种方法可以重复使用相同的代码来创建许多具有类似行为的模拟?
由于JAXB生成类是没有任何商业行为简单的DTO你不应该嘲笑他们...... –
因为构建它们需要其他对象(Qname名称,类 declaredType,Class scope,T值),并且它的可读性会下降。无论如何,我可以做到这一点,但我遇到了这个例外,我想了解它。 –
marco