Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导集成测试-端口绑定_Java_Spring Boot_Junit - Fatal编程技术网

Java Spring引导集成测试-端口绑定

Java Spring引导集成测试-端口绑定,java,spring-boot,junit,Java,Spring Boot,Junit,我正试图为我编写的微服务编写一个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

我正试图为我编写的微服务编写一个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端口已经在使用中。我知道它正在使用,因为microservice绑定到端口,但我不明白为什么WebEnvironment=RANDOM\u port没有为测试服务器分配与8888不同的端口


有人能帮忙吗?

我将随机端口选项更改为已定义的端口,从而解决了这个问题。这强制Spring引导使用配置文件中指定的端口。出于某种原因,我不确定为什么,随机端口选项一直选择使用8888,即使它正在使用。

您是否可以分享错误StackTrace您在测试期间是否尝试检查
server.PORT
的值?