【发布时间】:2019-12-02 16:43:41
【问题描述】:
我想在 Spring Boot 集成测试中使用 TestRestTemplate 测试我的 REST 端点,但我不想一直将 "http://localhost" + serverPort + "/" 作为每个请求的前缀。 Spring 可以使用正确的根 url 配置 TestRestTemplate-bean 并将其自动装配到我的测试中吗?
我不希望它看起来像这样:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class FoobarIntegrationTest {
@LocalServerPort
private int port;
private TestRestTemplate testRestTemplate = new TestRestTemplate();
@Test()
public void test1() {
// out of the box I have to do it like this:
testRestTemplate.getForEntity("http://localhost:" + port + "/my-endpoint", Object.class);
// I want to do it like that
//testRestTemplate.getForEntity("/my-endpoint", Object.class);
}
}
【问题讨论】:
标签: spring-boot integration-testing spring-test