【问题标题】:How to use @RestTemplateClient with JUnit 5?如何在 JUnit 5 中使用 @RestTemplateClient?
【发布时间】: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


    【解决方案1】:

    我使用最后一个 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"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.zpavel</groupId>
        <artifactId>test</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mariadb.jdbc</groupId>
                <artifactId>mariadb-java-client</artifactId>
                <version>2.4.3</version>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
                <version>1.4.199</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.8</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                    </exclusion>
                </exclusions>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    

    application.properties(我使用了 h2 内存数据库):

    spring.datasource.url=jdbc:h2:mem:testdb
    spring.datasource.driverClassName=org.h2.Driver
    spring.datasource.username=sa
    spring.datasource.password=password
    spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
    

    FooService.java:

    @Service
    public class FooService {
    
        private final RestTemplate restTemplate;
    
        public FooService(RestTemplateBuilder restTemplateBuilder) {
            this.restTemplate = restTemplateBuilder.build();
        }
    
        public String getIndex() {
            String result = restTemplate.getForObject("http://localhost:8080", String.class);
            System.out.println("index: " + result);
            return result;
        }
    }
    

    FooServiceTest.java:

    @RestClientTest(FooService.class)
    public class FooServiceTest {
    
        @Autowired
        private FooService service;
    
        @Autowired
        private MockRestServiceServer server;
    
        @Test
        public void testIndex() throws Exception {
            this.server.expect(requestTo("http://localhost:8080")).andRespond(withSuccess("hello", MediaType.TEXT_PLAIN));
            String greeting = this.service.getIndex();
            assertEquals(greeting, "hello");
        }
    }
    

    【讨论】:

    • 请注意导入:import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;导入静态 org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;导入静态 org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
    【解决方案2】:

    spring MockMvc 对你有用吗?

    @Autowired
    private MockMvc mockMvc;
    
    public void test() {
        this.mockMvc.perform(get("/form")
                .header(AUTHORIZATION, authHeader))
                .andDo(print())
                .andExpect(status().isOk());
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 2020-04-20
      • 2016-11-29
      相关资源
      最近更新 更多