gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:15 25 4
gpt4 key购买 nike

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

最佳答案

是的,你可以。

正常实例化您的 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);

关于java - 在服务器端测试 Spring Web 服务端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20001493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com