【问题标题】:TestRestTemplate - migrating from spring boot 1.5 to 2.0TestRestTemplate - 从 spring boot 1.5 迁移到 2.0
【发布时间】:2018-08-17 20:54:24
【问题描述】:

当我将我的代码库从 Spring Boot 1.5 迁移到 2.0 时,我发现在我的测试中从 1.5 开始的这个构造函数在 2.0 中被完全删除了。 & 他们似乎不是 Spring Boot 2.0 的替代品?

TestRestTemplate 模板 = new TestRestTemplate(restTemplate.getRestTemplate(),"username","password");

类: org.springframework.boot.test.web.client.TestRestTemplate

谢谢

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    现在需要传递RestTemplateBuilder 而不是RestTemplate

    https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/web/client/TestRestTemplate.html

    TestRestTemplate(RestTemplateBuilder restTemplateBuilder, String username, String password, TestRestTemplate.HttpClientOption... httpClientOptions) 使用指定的凭据创建一个新的 TestRestTemplate 实例。

    【讨论】:

    • 谢谢我试过了,但是我的测试一直抛出异常:原因:org.apache.http.ProtocolException:未指定目标主机
    • @Autowired protected RestTemplateBuilder restTemplateBuilder;
    • 这似乎与 RestTemplateBuilder 我必须以某种方式指定目标主机和端口,在 1.5 中我没有
    • 如果我改成这个,我现在发现了它:@SpringBootTest(classes = AuthApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT) 然后在我的测试中如果我添加localhost:<myport>测试一切正常。唯一的问题是我不想在我的测试中使用固定端口,在 1.5 中我能够拥有 random_port...
    猜你喜欢
    • 2019-04-11
    • 2018-09-18
    • 2019-10-22
    • 2019-03-24
    • 2019-06-26
    • 2018-12-11
    • 1970-01-01
    • 2021-05-21
    • 2018-08-23
    相关资源
    最近更新 更多