【问题标题】:Testing Spring web services end point at server side?在服务器端测试 Spring Web 服务端点?
【发布时间】:2013-11-15 12:49:32
【问题描述】:

我正在使用 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 传递 吗?如何对我的端点进行集成测试?

【问题讨论】:

    标签: java spring web-services jaxb spring-ws


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

      这些天我遇到了同样的问题,我这样解决了(我使用您的对象名称进行请求和响应):

      请注意,您必须启动并运行服务

      集成测试.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();
              }
          }
      
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2010-10-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-29
          • 1970-01-01
          • 2022-01-16
          • 1970-01-01
          相关资源
          最近更新 更多