我们在Sping Boot 3.2.0中通过@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
和WebTestClient
进行的集成测试失败,但有以下例外
org.springframework.web.reactive.function.client.WebClientRequestException:
Connection refused: no further information: /127.0.0.1:80
字符串
测试如下所示:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestClass {
@Autowired
protected WebTestClient webTestClient;
@Test
void testShiftLog() {
// Given
URI uri = UriComponentsBuilder.fromUri(URI.create(SHIFTLOG_ENDPOINT_URL))
.build().toUri();
ShiftLogItem body = generateSchichtbuchItem();
// When, Then
webTestClient.post().uri(uri)
.body(Mono.just(body), ShiftLogItem.class)
.header("Content-Type", "application/json")
.exchange() // HERE THE EXCEPTION IS THROWN.
.expectStatus().isCreated()
.expectBody(Void.class);
}
}
型
当我调试代码时,我可以看到WebTestClient
对象配置正确,即随机端口。尽管如此,例外引用 localhost:80,这完全是奇怪的。有什么想法吗?
1条答案
按热度按时间o8x7eapl1#
现在问题已经解决了。这里的问题是
.uri(URI uri)
部分。如果你像这里一样应用了一个基URI,那么.uri(URI uri)
将不会应用于基URI,而是取而代之。因此,为了修复它,你必须使用重载的
.uri(String uri)
方法。你可以通过URI的toString()
方法轻松完成:字符串
另请参阅Spring Java文档:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/reactive/server/WebTestClient.UriSpec.html#uri(java.net.URI)