我使用的是Spring Boot 2.1.7.RELEASE和JUnit 5。不幸的是,我在@RestClientTest中遇到了问题,因为我收到了java.lang.IllegalStateException:由于未将MockServerRestTemplateCustomizer绑定到RestTemplate,无法使用自动配置的MockRestServiceServer。您有任何想法如何正确配置它吗?
类似的代码在JUnit 4中完美运行。 源代码基于文档https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html。
源代码:
类似的代码在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)```