spring ws-client的单元测试 - 检查是否正在调用marshalSendAndReceive

时间:2017-08-27 18:29:08

标签: java spring unit-testing spring-security spring-ws

我的目标是为我的WebServiceClientImpl类编写一个单元测试,我想添加一个检查是否正在调用marshalSendAndReceive的测试。我使用Mockitopowermockito框架。我需要使用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)

任何人都可以帮助进行单元测试吗?任何帮助将不胜感激 - 谢谢!

0 个答案:

没有答案