本文介绍了Spring Boot Integration测试随机空闲端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够获得Spring Boot集成,以生成一个随机的自由端口来启动自身.但是我还需要一个Redis的免费端口.
I am able to get Spring Boot integration to generate a random free port to launch itself on. But I also need a free port for Redis.
@ContextConfiguration(classes = {MyApplication.class}, loader = SpringApplicationContextLoader.class)
@WebIntegrationTest(randomPort = true, value = "server.port:0")
@ActiveProfiles(profiles = {"local"})
public class SegmentSteps {
private static final String HOST_TEMPLATE = "http://localhost:%s";
// Needs to be a random open port
private static final int REDIS_PORT = 6380;
private String host;
@Value("${local.server.port}")
private int serverPort;
private RedisServer redisServer;
@Before
public void beforeScenario() throws Exception {
host = String.format(HOST_TEMPLATE, serverPort);
redisServer = RedisServer.builder()
.redisExecProvider(RedisExecProvider.defaultProvider())
.port(REDIS_PORT)
.setting("bind 127.0.0.1")
.build();
redisServer.start();
}
...
}
关于如何实现这一目标的任何想法?
Any ideas on how to achieve this?
推荐答案
您可以使用Spring Framework的 SocketUtils
以获得可用端口:
You can use Spring Framework's SocketUtils
to get an available port:
int redisPort = SocketUtils.findAvailableTcpPort();
这篇关于Spring Boot Integration测试随机空闲端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!