我用硬编码的 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)