我正在使用Spring WS 2.0。我已经看到了下面的终点和测试用例来测试终点。
@Endpoint
public class CustomerEndpoint {
@ResponsePayload
public CustomerCountResponse getCustomerCount(
@RequestPayload CustomerCountRequest request) {
CustomerCountResponse response = new CustomerCountResponse();
response.setCustomerCount(10);
return response;
}
}
import javax.xml.transform.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.xml.transform.StringSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.ws.test.server.MockWebServiceClient;
import static org.springframework.ws.test.server.RequestCreators.*;
import static org.springframework.ws.test.server.ResponseMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-ws-servlet.xml")
public class CustomerEndpointIntegrationTest {
@Autowired
private ApplicationContext applicationContext;
private MockWebServiceClient mockClient;
@Before
public void createClient() {
mockClient = MockWebServiceClient.createClient(applicationContext);
}
@Test
public void customerEndpoint() throws Exception {
Source requestPayload = new StringSource(
"<customerCountRequest xmlns='http://springframework.org/spring-ws'>" +
"<customerName>John Doe</customerName>" +
"</customerCountRequest>");
Source responsePayload = new StringSource(
"<customerCountResponse xmlns='http://springframework.org/spring-ws'>" +
"<customerCount>10</customerCount>" +
"</customerCountResponse>");
mockClient.sendRequest(withPayload(requestPayload)).
andExpect(payload(responsePayload));
}
}
在这里,我有关于测试用例的查询。这里我们传递 XML字符串作为请求有效负载。但在我的情况下,我有非常大的XML文件,将有100行。在这种情况下,我感觉不是传递XML字符串我可以将JAXB生成的对象(CustomerCountRequest)本身作为requestPayload 传递吗?如何对我的终点进行集成测试?
答案 0 :(得分:2)
是的,你可以。
正常实例化您的CustomerCountRequest对象,并使用JAXBContext将其包装在JAXBSource中:
CustomerCountRequest request = new CustomerCountRequest();
// add setters on the request object if needed
JAXBContext jc = JAXBContext.newInstance(CustomerCountRequest.class);
JAXBSource source = new JAXBSource(jc, request);
答案 1 :(得分:1)
这些天我遇到了同样的问题,我解决了这个问题(我使用你的对象名称作为请求和响应):
请注意,您必须启用并运行服务
集成-的test.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="defaultUri" value="http://localhost:8080/MyServices/ws"/>
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="it.ws.soap" />
</bean>
</beans>
CustomerEndpointIntegrationTest
/**
* Inspired by: http://docs.spring.io/spring-ws/site/reference/html/client.html
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:integration-test.xml")
public class CustomerEndpointIntegrationTest extends AbstractJUnit4SpringContextTests {
@Autowired
private WebServiceTemplate webServiceTemplate;
@Before
public void startServer() {
webServiceTemplate.setCheckConnectionForError(false);
webServiceTemplate.setCheckConnectionForFault(false);
}
@Test
public void testOne() throws Exception {
CustomerCountRequest request = (CustomerCountRequest) loadRequest("MyRequestBody.xml");
CustomerCountResponse response = (CustomerCountResponse) webServiceTemplate.marshalSendAndReceive(request);
Assert.assertNotNull(response);
}
@Test
public void testTwo() throws Exception {
CustomerCountRequest request = (CustomerCountRequest) loadRequest("MyRequestBodyTwo.xml");
try {
webServiceTemplate.marshalSendAndReceive(request);
Assert.fail();
} catch (SoapFaultClientException ex) {
Assert.assertEquals("Validation error", ex.getSoapFault().getFaultStringOrReason());
}
}
private Object loadRequest(String requestName) throws Exception {
String file = getClass().getClassLoader().getResource(requestName).getFile();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
return webServiceTemplate.getUnmarshaller().unmarshal(new StreamSource(fis));
} finally {
fis.close();
}
}
}
答案 2 :(得分:0)
我们面临着类似的问题,我们解决了从类路径可访问的位置读取xml文件的问题。如果您需要更改测试用例,至少不必重写字符串。