Spring Boot Integration test random free port

断了今生、忘了曾经 提交于 2019-12-14 00:37:23

问题


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?


回答1:


You can use Spring Framework's SocketUtils to get an available port:

int redisPort = SocketUtils.findAvailableTcpPort();



回答2:


You could also run your Redis within Docker by either using a Java client of your choosing or by leveraging Overcast. If using Overcast, by activating exposeAllPorts option, your Redis would be bound to a random port on the host machine.

As for how you can enable the property in the context - it would require some work but you could implement a listener that would start up Docker containers and put the ports as properties in an environment:

public class IntegrationTestBootstrapApplicationListener implements
    ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {

    public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 4;
    public static final int PROPERTY_SOURCE_NAME = "integrationTestProps";

    private int order = DEFAULT_ORDER;

    public void setOrder(int order) {
        this.order = order;
    }

    @Override
    public int getOrder() {
        return this.order;
    }

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        ConfigurableEnvironment environment = event.getEnvironment();

        if (!environment.getPropertySources().contains(PROPERTY_SOURCE_NAME)) {
            CloudHost itestHost = CloudHostFactory.getCloudHost("redis");
            itestHost.setup();

            String host = itestHost.getHostName();
            // fetch the dynamic port from Docker
            int port = itestHost.getPort(6379); 

            // alternatively, skip the whole CloudHost setup above and just use:
            // int port = SocketUtils.findAvailableTcpPort();

            environment.getPropertySources().addLast(
              new MapPropertySource(
                PROPERTY_SOURCE_NAME, Collections.<String, Object> singletonMap(
                  "redis.port", port));
            );
        }
    }

}


来源:https://stackoverflow.com/questions/38977633/spring-boot-integration-test-random-free-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!