我能夠獲得Spring Boot集成以生成隨機自由端口以啓動自身。但是我還需要一個Redis的免費端口。Spring Boot集成測試隨機免費端口
@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();
}
...
}
有關如何實現此目的的任何想法?
是有沒有辦法在上下文中獲得相同的數字? – ptimson
或者我只是使用一個int bean?有限定符? – ptimson
@ptimson,我不明白你的問題。你能詳細說明一下嗎? – mre