SOAP 서비스에서 다른 응답을받을 때 클래스의 동작을 검증하기위한 테스트를 작성하고 있습니다. 나는 JAXB, 그래서 내 대답은 JaxbElements
포함되어 있으며, 그들 중 많은 사람들을 위해, 나는 그와 같은 모의를 작성해야 사용모의 객체 모의 메소드 내
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;
}
합니다. I 리팩토링 후 코드를 실행할 때 불행히도, I이 오류 :
라인 126mockWithValue
방법의 제 호출이다
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
.
그래서 질문 : 비슷한 행동으로 많은 모의를 만들기 위해 동일한 코드를 재사용 할 수있는 방법이 있습니까?
클래스는 모든 비즈니스 행위없이 간단한 DTO들입니다 당신은 그들을 조롱하면 안된다 ... –
그들에게 다른 객체 (Qname 이름, 클래스 declaredType, 클래스 스코프, T 값)가 필요하므로 가독성이 떨어진다. 어쨌든, 나는 그것을 할 수 있지만, 나는이 예외에 부딪 쳤고, 나는 그것을 이해하고 싶다. –
marco