编程语言
首页 > 编程语言> > java-在服务器端测试Spring Web Services端点?

java-在服务器端测试Spring Web Services端点?

作者:互联网

我正在使用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行.在那种情况下,我觉得可以传递JAXB生成的对象(CustomerCountRequest)本身作为requestPayload,而不是传递XML字符串?如何进行集成测试到终点?

解决方法:

是的你可以.

照常实例化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);

标签:spring-ws,jaxb,web-services,spring,java
来源: https://codeday.me/bug/20191122/2060195.html