Java SpringBoot2.1.1中带有随机_端口的SpringBootTest每次都使用8080
我有一个在Spring Boot 2.1.1和Java 11中运行的测试类,无论我做什么,它都在端口8080上运行:Java SpringBoot2.1.1中带有随机_端口的SpringBootTest每次都使用8080,java,spring,spring-boot,spring-boot-test,Java,Spring,Spring Boot,Spring Boot Test,我有一个在Spring Boot 2.1.1和Java 11中运行的测试类,无论我做什么,它都在端口8080上运行: import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServer
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RunWith( SpringJUnit4ClassRunner.class )
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = TestClass.Config.class
)
@ContextConfiguration(
classes = TestClass.Config.class
)
@TestPropertySource( properties = "server.port=0" )
public class TestClass
{
@LocalServerPort
private String port;
@Test
public void testPort() throws Exception
{
mockMvc
.perform(
MockMvcRequestBuilders.get( "/" )
)
.andExpect( MockMvcResultMatchers.status().isOk() );
}
@Configuration
@RestController
public static class Config
{
@Bean
ServletWebServerFactory servletWebServerFactory()
{
return new TomcatServletWebServerFactory();
}
@GetMapping( "/" )
public String test(HttpServletRequest request, HttpServletResponse response)
{
//This still shows no random port
System.out.println( request.getLocalPort() );
return "ok";
}
}
}
即使我试着这样做:
@Bean
ServletWebServerFactory servletWebServerFactory()
{
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort( SocketUtils.findAvailableTcpPort() );
return factory;
}
这确实会导致字段port
具有随机端口号,MockMvc仍然使用我的控制器的默认端口
如何让它使用随机端口?尝试将端口设置为0,这对我很有效
@Bean
ServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory(0);
}
运行两次测试将生成两个不同的随机端口
当您自己配置它时,您正在绕过Spring引导,因此它无法检测端口。删除
ServletWebServerFactory
的@Bean
和@TestPropertySource(properties=“server.port=0”)
看起来您在这里做了很多工作。另外,为什么您的@配置
也是@RestController
@M.Deinum删除这些(并添加@EnableAutoConfiguration
-否则它会失败)会导致相同的问题。您不应该有单独的配置文件,而只应该指向您的spring boot应用程序类。当我使用它并在控制器的端点方法中放置断点时,我得到:request.getLocalPort()=>80
。字段port
确实显示了一个随机数,但MockMvcTry没有使用它:mockMvc.perform(MockMvcRequestBuilders.get(“/”))和expect(MockMvcResultMatchers.status().isOk())
您看到的是端口80,因为request.getLocalPort()
正在从实例MockHttpServletRequest
获取值,该实例的常量端口值为80<代码>公共静态最终int默认值\服务器\端口=80代码>