测试 Spring JAX-WS Web 服务
Testing Spring JAX-WS Web Services
我有一个使用 SimpleJaxWsServiceExporter
导出的 Web 服务 (@WebService
)。
我想为其创建集成测试。
我该怎么做?可以使用 spring-ws-test
项目中的 MockWebServiceClient
class 吗?
不,MockWebServiceClient
仅适用于 Spring-WS 服务。
您可以创建客户端或服务器端测试用例。
我更喜欢服务器端测试,因为您根本不必部署应用程序。应该是容器外测试。
SEREVR SIDE Spring-WS Test (NO DEPLOYMENT REQUIRED) - Keep in mind the following points.
import org.springframework.ws.test.server.MockWebServiceClient
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = YOUR CONTEXT XML FILE LOCATION
/**
* <strong>Main entry point for server-side Web service testing</strong>. Typically used to test a {@link
* org.springframework.ws.server.MessageDispatcher MessageDispatcher} (including its endpoints, mappings, etc) by
* creating request messages, and setting up expectations about response messages.
*/
private MockWebServiceClient mockServiceClient;
@Before
public void setUp() throws Exception{
mockServiceClient = MockWebServiceClient.createClient(applicationContext);
}
@Test
mockServiceClient.sendRequest(withPayload(VALID_PAYLOAD_REQUEST)).andExpect(VALID_PAYLOAD_RES)
CLIENT SIDE Spring-WS TEST - YOU HAVE TO DEPLOY EAR or WAR
public class CountryServiceClient extends WebServiceGatewaySupport
String uri = "http://localhost:8080/Endpointaddress/url
request = User.class/ anythig else/ make the bean ready
Object response = getWebServiceTemplate().marshalSendAndReceive(uri, request, new WebServiceMessageCallback() {
//real call to webservice make sure your application is up and running with the above uri.
@Override
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
MimeHeaders mimeHeader = soapMessage.getSaajMessage().getMimeHeaders();
mimeHeader.setHeader("ApplicationId", "Z1012922");
mimeHeader.setHeader("userid", "aacom");
}
});
我有一个使用 SimpleJaxWsServiceExporter
导出的 Web 服务 (@WebService
)。
我想为其创建集成测试。
我该怎么做?可以使用 spring-ws-test
项目中的 MockWebServiceClient
class 吗?
不,MockWebServiceClient
仅适用于 Spring-WS 服务。
您可以创建客户端或服务器端测试用例。
我更喜欢服务器端测试,因为您根本不必部署应用程序。应该是容器外测试。
SEREVR SIDE Spring-WS Test (NO DEPLOYMENT REQUIRED) - Keep in mind the following points.
import org.springframework.ws.test.server.MockWebServiceClient
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = YOUR CONTEXT XML FILE LOCATION
/**
* <strong>Main entry point for server-side Web service testing</strong>. Typically used to test a {@link
* org.springframework.ws.server.MessageDispatcher MessageDispatcher} (including its endpoints, mappings, etc) by
* creating request messages, and setting up expectations about response messages.
*/
private MockWebServiceClient mockServiceClient;
@Before
public void setUp() throws Exception{
mockServiceClient = MockWebServiceClient.createClient(applicationContext);
}
@Test
mockServiceClient.sendRequest(withPayload(VALID_PAYLOAD_REQUEST)).andExpect(VALID_PAYLOAD_RES)
CLIENT SIDE Spring-WS TEST - YOU HAVE TO DEPLOY EAR or WAR
public class CountryServiceClient extends WebServiceGatewaySupport
String uri = "http://localhost:8080/Endpointaddress/url
request = User.class/ anythig else/ make the bean ready
Object response = getWebServiceTemplate().marshalSendAndReceive(uri, request, new WebServiceMessageCallback() {
//real call to webservice make sure your application is up and running with the above uri.
@Override
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
MimeHeaders mimeHeader = soapMessage.getSaajMessage().getMimeHeaders();
mimeHeader.setHeader("ApplicationId", "Z1012922");
mimeHeader.setHeader("userid", "aacom");
}
});