Spring WebServiceTemplate 的 JUnit 不工作
JUnit for Spring WebServiceTemplate not working
我正在使用 Spring WebServiceTemplate 使用 SOAP Web 服务。在执行 JUnit 时我遇到了一个问题。
我的客户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();
}}
我的测试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();
}}
问题:
当我执行我的测试用例时,我在客户端 class 中收到行 "webServiceTemplate.marshalSendAndReceive(request)" 的 NullPointerException。
webServiceTemplate 为空。
知道我做错了什么吗?
我通过将 "MyWsClient myWsClient" 上的 @InjectMocks 更改为 @Autowired 来解决它;
我正在使用 Spring WebServiceTemplate 使用 SOAP Web 服务。在执行 JUnit 时我遇到了一个问题。
我的客户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();
}}
我的测试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();
}}
问题:
当我执行我的测试用例时,我在客户端 class 中收到行 "webServiceTemplate.marshalSendAndReceive(request)" 的 NullPointerException。
webServiceTemplate 为空。
知道我做错了什么吗?
我通过将 "MyWsClient myWsClient" 上的 @InjectMocks 更改为 @Autowired 来解决它;