简体   繁体   English

Spring WebServiceTemplate的JUnit不起作用

[英]JUnit for Spring WebServiceTemplate not working

I am consuming a SOAP Webservice using Spring WebServiceTemplate. 我正在使用Spring WebServiceTemplate使用SOAP Web服务。 While executing the JUnit I am facing an issue. 在执行JUnit时,我遇到了一个问题。

My Client class: 我的客户班:

@Service
public class MyWsClient extends WebServiceGatewaySupport {

@Autowired
private WebServiceTemplate webServiceTemplate;

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

My Test class: 我的测试班:

@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();
}}

Issue: 问题:
When i execute my test case I am getting NullPointerException for line "webServiceTemplate.marshalSendAndReceive(request)" inside my Client class. 当我执行测试用例时,我的Client类中的“ webServiceTemplate.marshalSendAndReceive(request)”行出现了NullPointerException异常。
The webServiceTemplate is coming as null. webServiceTemplate即将为空。
Any idea what I am doing wrong? 知道我在做什么错吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM