第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 JUnit 5 中使用@RestTemplateClient?

如何在 JUnit 5 中使用@RestTemplateClient?

富國滬深 2023-06-08 20:54:36
我使用 spring boot 2.1.7.RELEASE 和 junit 5。不幸的是,@RestClientTest 有問題,因?yàn)槲沂盏?java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since MockServerRestTemplateCustomizer has not being bound to a RestTemplate。您對如何正確配置有任何想法嗎?類似的代碼在 junit 4 中完美運(yùn)行。源代碼基于文檔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");    }
查看完整描述

2 回答

?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個贊

我用最后一個 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 內(nèi)存數(shù)據(jù)庫):


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");

    }

}


查看完整回答
反對 回復(fù) 2023-06-08
?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個贊

spring MockMvc 會為你工作嗎?


@Autowired

private MockMvc mockMvc;


public void test() {

    this.mockMvc.perform(get("/form")

            .header(AUTHORIZATION, authHeader))

            .andDo(print())

            .andExpect(status().isOk());

}


查看完整回答
反對 回復(fù) 2023-06-08
  • 2 回答
  • 0 關(guān)注
  • 154 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號