2013-11-15 78 views
3

我正在使用Spring WS 2.0。我已经看到下面的结束点和测试用例来测试结束点。在服务器端测试Spring Web服务端点?

@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

我们都面临着类似的问题,我们解决阅读从classpath中访问的位置xml文件。如果您需要更改测试用例,至少您不必重写字符串。

1

是的,你可以。

实例化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

我有同样的问题,这些天,我解决了这样的(我用你的对象名称请求和响应) :

注意,你必须有服务正常运行

集成-的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(); 
     } 
    } 

}