我的目标是为我的WebServiceClientImpl
类编写一个单元测试,我想添加一个检查是否正在调用marshalSendAndReceive
的测试。我使用Mockito
和powermockito
框架。我需要使用powermockito
,因为WebServiceGatewaySupport.getWebServiceTemplate
是最终方法,mockito
无法做到。
这是我要测试的课程。
public class WebServiceClientImpl extends WebServiceGatewaySupport implements
WebServiceClient {
@Override
public <T> T execute(final Object requestPayload,
final Class<T> responseType,
final String soapActionCallBack) {
return (T) getWebServiceTemplate()
.marshalSendAndReceive(requestPayload, new
SoapActionCallback(soapActionCallBack));
}
到目前为止,这是我的单元测试。
RunWith( PowerMockRunner.class )
@PowerMockRunnerDelegate( HierarchicalContextRunner.class )
@PrepareForTest( WebServiceClientImpl.class )
public class WebServiceClientImplTest {
private WebServiceClientImpl webServiceClientImpl = PowerMockito.mock(WebServiceClientImpl.class);
@Mock
private WebServiceTemplate webServiceTemplate;
@Before
public void before() {
MockitoAnnotations.initMocks( this );
}
public class Execute {
Object response = "response";
Object request = "request";
SoapActionCallback soapActionCallBack = new SoapActionCallback("soap");
@Before
public void before() {
when( webServiceClientImpl.getWebServiceTemplate() ).thenReturn( webServiceTemplate );
when( webServiceTemplate.marshalSendAndReceive ( request, soapActionCallBack) ).thenReturn(response);
response = webServiceClientImpl.execute( request, Object.class, "soapActionCallBack" );
System.out.println ("RESPONSE " + response );
}
@Test
public void marshalSendAndReceiveInvoked() {
verify( webServiceTemplate ).marshalSendAndReceive( request, soapActionCallBack );
}
@Test
public void webServiceClientReturnValue() {
assertEquals( "response", response );
}}}
然而,即使我把它放在单元测试中,响应仍然是空的。
when( webServiceTemplate.marshalSendAndReceive ( request, soapActionCallBack) ).thenReturn(response)
任何人都可以帮助进行单元测试吗?任何帮助将不胜感激 - 谢谢!