我正在使用 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 即将为空。
知道我做错了什么吗?