【发布时间】:2019-12-23 14:51:10
【问题描述】:
我使用 spring boot 2.1.7.RELEASE 和 junit 5。不幸的是,@RestClientTest 有问题,因为我收到 java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since MockServerRestTemplateCustomizer has not be bound to一个休息模板。您对如何正确配置有任何想法吗?
类似的代码在junit 4中完美运行。 源代码基于文档https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html。
源代码:
@RestClientTest(RemoteVehicleDetailsService.class)
public class ExampleRestClientTest {
@Autowired
private RemoteVehicleDetailsService service;
@Autowired
private MockRestServiceServer server;
@Test
public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()
throws Exception {
this.server.expect(requestTo("/greet/details"))
.andRespond(withSuccess("hello", MediaType.TEXT_PLAIN));
String greeting = this.service.callRestService();
assertThat(greeting).isEqualTo("hello");
}
例外:
java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since MockServerRestTemplateCustomizer has not been bound to a RestTemplate
at org.springframework.util.Assert.state(Assert.java:73)
at org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerAutoConfiguration$DeferredRequestExpectationManager.getDelegate(MockRestServiceServerAutoConfiguration.java:110)
at org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerAutoConfiguration$DeferredRequestExpectationManager.expectRequest(MockRestServiceServerAutoConfiguration.java:87)
at org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:107)
at org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:92)
at ExampleRestClientTest.getVehicleDetailsWhenResultIsSuccessShouldReturnDetails(ExampleRestClientTest.java:27)```
【问题讨论】:
标签: java spring spring-boot junit spring-boot-test