gpt4 book ai didi

unit-testing - 使用 ClientHttpRequestInterceptor 对 Rest 客户端进行单元测试

转载 作者:行者123 更新时间:2023-12-04 05:28:10 25 4
gpt4 key购买 nike

下面的单元测试失败。我正在打印请求和响应,并且可以确认 MockRestServiceServer 在调用端点时返回模拟的 JSON。当我将测试更改为直接与服务器通信时,单元测试通过。不知道我做错了什么

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {Application.class, Beans.class})
public class BillingSystemClientImplTest {

private MockRestServiceServer mockServer;

@Autowired
private RestTemplate restTemplate;

@Autowired
private Properties properties;

@Autowired
private BillingSystemClient client;

@Before
public void setUp() {
mockServer = MockRestServiceServer.createServer(restTemplate);
}

@Test
public void testGetAccount() throws Exception {

Resource resource = new ClassPathResource("/account.json", getClass());

UriComponents uri = UriComponentsBuilder.fromUriString(properties.getAccountResource())
.buildAndExpand("53737803");

mockServer.expect(requestTo(uri.toUriString()))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));

AccountResponse response = client.getAccount("53737803");

Assert.assertNotNull(response.getAccountNumber());

mockServer.verify();

}

}

下面的异常提示响应对象为空。这个对象是从响应映射的
    response = restTemplate.getForObject(uriComponents.toUri(), AccountResponse.class);

和异常(exception)
java.lang.NullPointerException
at com.something.ws.client.BillingSystemClientImplTest.testGetAccount(BillingSystemClientImplTest.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)

事实证明这是因为我如何设置我的 restTemplate
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
BufferingClientHttpRequestFactory bufferingClientHttpRequestFactory = new BufferingClientHttpRequestFactory(requestFactory);
requestFactory.setOutputStreaming(false);

RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(mpxLoggingRequestInterceptor());
restTemplate.setErrorHandler(mpxErrorHandler());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
restTemplate.getInterceptors().add(new GenericRequestInterceptor());
restTemplate.setRequestFactory(bufferingClientHttpRequestFactory);

如果我删除 RequestFactory 和 Interceptor 测试将通过。这仅是单元测试的问题。实际代码在生产中顺利运行

最佳答案

您需要启用响应正文的重复读取,因此在创建 MockRestServiceServer 时调用 bufferContent() 如下:

        mockServer = MockRestServiceServer
.bindTo(restTemplate)
.ignoreExpectOrder(true)
.bufferContent()
.build();

关于unit-testing - 使用 ClientHttpRequestInterceptor 对 Rest 客户端进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39914325/

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