小编Kav*_*lai的帖子

为什么 RANDOM_PORT 不可分配?

我用硬编码的 url 进行了一些测试baseUrl = "http://localhost:8080/api/v1/"。当我尝试将其交换为 时baseUrl = "http://localhost:" + port + "/api/v1/",我得到

Caused by: java.net.NoRouteToHostException: 
Can't assign requested address (Address not available).
Run Code Online (Sandbox Code Playgroud)

这里有什么问题呢?

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class UrlShorteningControllerTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @LocalServerPort
    private int port;

    private String baseUrl = "http://localhost:" + port + "/api/v1/";
    // private String baseUrl = "http://localhost:8080/api/v1/";  // THIS WORKS FINE

        @Test
        void findAndRedirectHappyPath() throws URISyntaxException {
        final String dishpodUrl = baseUrl + "dishpods";

        URI uri = new URI(dishpodUrl);

        ResponseEntity<String> result …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-test

2
推荐指数
1
解决办法
737
查看次数

标签 统计

spring ×1

spring-mvc ×1

spring-test ×1