繁体   English   中英

Spring Boot集成测试-端口绑定

[英]Spring Boot Integration Testing - Port Bound

我正在尝试为我编写的微服务编写Spring Boot集成测试。 微服务在端口8888上运行。

这是我的测试课:

import com.mydomain.app.MyAPI;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyAPI.class}, webEnvironment = WebEnvironment.RANDOM_PORT)
public class MyIntegrationTest {

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Test
    public void contextLoads() {
    }

}

当我在不启动微服务的情况下运行测试时,测试方法可以正常通过-上下文加载。 问题是当我启动微服务然后运行测试时。 我收到一个错误消息,指出端口8888已被使用。 我知道它正在使用,因为微服务已绑定到端口,但是我不明白为什么WebEnvironment = RANDOM_PORT没有为测试服务器分配与8888不同的端口。

有人可以帮忙吗?

我设法通过将RANDOM_PORT选项更改为DEFINED_PORT来解决此问题。 这迫使Spring Boot使用配置文件中指定的端口。 由于某些原因,我不确定为什么,即使正在使用RANDOM_PORT选项,它仍继续选择使用8888。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM