1

我正在使用 Spring WebServiceTemplate 使用 SOAP Webservice。在执行 JUnit 时,我遇到了一个问题。

我的客户类:

@Service
public class MyWsClient extends WebServiceGatewaySupport {

@Autowired
private WebServiceTemplate webServiceTemplate;

public String getData(...) throws Exception{
    ...
    SampleResponse response = (SampleResponse) webServiceTemplate.marshalSendAndReceive(request);
    ...
    return response.getData();
}}

我的测试课:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-junit.xml"})
public class MyWsClientTest {

@Autowired
ApplicationContext applicationContext;

@InjectMocks
MyWsClient myWsClient;

private MockWebServiceServer mockServer;

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    mockServer = MockWebServiceServer.createServer(applicationContext);
}

@Test
public void testGetData() throws Exception {

    Source expectedRequestPayload = new StringSource(
            "<customerCountRequest xmlns=\"http://springframework.org/spring-ws/test\" />");
    Source responsePayload = new StringSource(
            "<customerCountResponse xmlns='http://springframework.org/spring-ws/test'>"
                    + "<customerCount>10</customerCount>" + "</customerCountResponse>");
    mockServer.expect(RequestMatchers.payload(expectedRequestPayload)).andRespond(ResponseCreators.withPayload(responsePayload));

    String data = myWsClient.getData(...);
    assertNotNull(data);
    mockServer.verify();
}}

问题:
当我执行我的测试用例时,我的客户端类中的“webServiceTemplate.marshalSendAndReceive(request)”行出现 NullPointerException。
webServiceTemplate 即将为空。
知道我做错了什么吗?

4

1 回答 1

1

我通过在“MyWsClient myWsClient”上将@InjectMocks 更改为@Autowired 来解决它;

于 2017-04-19T07:43:42.760 回答