如何在 JUnit 5 中使用@RestTemplateClient?

我使用 spring boot 2.1.7.RELEASE 和 junit 5。不幸的是,@RestClientTest 有问题,因为我收到 java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since MockServerRestTemplateCustomizer has not being bound to a RestTemplate。您对如何正确配置有任何想法吗?


类似的代码在 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");

    }


富国沪深
浏览 118回答 2
2回答

江户川乱折腾

我用最后一个 spring boot 2.1.7 和 junit 5 测试了你的案例,一切正常。请尝试 :pom.xml:<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <groupId>com.zpavel</groupId>&nbsp; &nbsp; <artifactId>test</artifactId>&nbsp; &nbsp; <version>1.0-SNAPSHOT</version>&nbsp; &nbsp; <parent>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-parent</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.1.7.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; <relativePath/>&nbsp; &nbsp; </parent>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-data-jpa</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-data-rest</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.mariadb.jdbc</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>mariadb-java-client</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.4.3</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.h2database</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>h2</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>runtime</scope>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.4.199</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.projectlombok</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>lombok</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.18.8</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <exclusions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <exclusion>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>junit</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </exclusion>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </exclusions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.junit.jupiter</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit-jupiter-engine</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies></project>application.properties(我使用了 h2 内存数据库):spring.datasource.url=jdbc:h2:mem:testdbspring.datasource.driverClassName=org.h2.Driverspring.datasource.username=saspring.datasource.password=passwordspring.jpa.database-platform=org.hibernate.dialect.H2DialectFooService.java :@Servicepublic class FooService {&nbsp; &nbsp; private final RestTemplate restTemplate;&nbsp; &nbsp; public FooService(RestTemplateBuilder restTemplateBuilder) {&nbsp; &nbsp; &nbsp; &nbsp; this.restTemplate = restTemplateBuilder.build();&nbsp; &nbsp; }&nbsp; &nbsp; public String getIndex() {&nbsp; &nbsp; &nbsp; &nbsp; String result = restTemplate.getForObject("http://localhost:8080", String.class);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("index: " + result);&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }}FooServiceTest.java :@RestClientTest(FooService.class)public class FooServiceTest {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private FooService service;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private MockRestServiceServer server;&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testIndex() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; this.server.expect(requestTo("http://localhost:8080")).andRespond(withSuccess("hello", MediaType.TEXT_PLAIN));&nbsp; &nbsp; &nbsp; &nbsp; String greeting = this.service.getIndex();&nbsp; &nbsp; &nbsp; &nbsp; assertEquals(greeting, "hello");&nbsp; &nbsp; }}

繁华开满天机

spring MockMvc 会为你工作吗?@Autowiredprivate MockMvc mockMvc;public void test() {&nbsp; &nbsp; this.mockMvc.perform(get("/form")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .header(AUTHORIZATION, authHeader))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .andDo(print())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .andExpect(status().isOk());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java