ptimson
ptimson

Reputation: 5813

Spring Boot Integration test random free port

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?

Upvotes: 11

Views: 11038

Answers (3)

Casey
Casey

Reputation: 6326

Here in 2021, this is the best way to do it I think. This requires spring-boot >= 2.3 / spring framework 5.2.5 and it uses @DynamicPropertySource annotation.

Helper class:

@TestConfiguration
public class TestRedisConfiguration
{
    private RedisServer redisServer;

    public TestRedisConfiguration(
            @Value("${spring.redis.port}")
                    int redisPort
    )
    {
        this.redisServer = new RedisServer(redisPort);
    }

    @PostConstruct
    public void postConstruct()
    {
        redisServer.start();
    }

    @PreDestroy
    public void preDestroy()
    {
        redisServer.stop();
    }

    static public void configurePort(DynamicPropertyRegistry r)
    {
        int port = SocketUtils.findAvailableTcpPort();
        r.add("spring.redis.host", () -> "localhost");
        r.add("spring.redis.port", () -> port);
    }
}

In your test class (or base class):

@SpringBootTest
@Import({TestRedisConfiguration.class})
public abstract class BaseTestFullContext
{
...
   @DynamicPropertySource
    static public void properties(DynamicPropertyRegistry r)
    {
        TestRedisConfiguration.configurePort(r);
    }
...

Upvotes: 1

Miloš Milivojević
Miloš Milivojević

Reputation: 5369

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));
            );
        }
    }

}

Upvotes: -1

Andy Wilkinson
Andy Wilkinson

Reputation: 116111

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

int redisPort = SocketUtils.findAvailableTcpPort();

Upvotes: 27

Related Questions