package com.example;

import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.testcontainers.containers.GenericContainer;

import javax.annotation.PostConstruct;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(DemoApplicationTest.TestConfiguration.class)
@WebIntegrationTest(randomPort = true)
public abstract class AbstractIntegrationTest {

    @ClassRule
    public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(6379);

    @Configuration
    @Import(DemoApplication.class)
    static class TestConfiguration {

        @Autowired
        ConfigurableEnvironment environment;

        @PostConstruct
        public void init() {
            MockPropertySource propertySource = new MockPropertySource("TestContainers");

            propertySource.setProperty("spring.redis.host", redis.getContainerIpAddress());
            propertySource.setProperty("spring.redis.port", redis.getMappedPort(6379));

            environment.getPropertySources().addFirst(propertySource);
        }
    }

    @Value("${local.server.port}")
    protected int port;
}

@@ -128,25 +128,21 @@
 otation.
-Autowired
+Value
 ;%0Aimport
@@ -167,37 +167,37 @@
 rk.b
-eans.factory.annotation.Value
+oot.test.EnvironmentTestUtils
 ;%0Aim
@@ -359,86 +359,37 @@
 ext.
-annot
+Applic
 ation
-.
 Con
-figuration;%0Aimport org.springframework.context.annotation.Import
+textInitializer
 ;%0Aim
@@ -419,14 +419,13 @@
 k.co
-re.env
+ntext
 .Con
@@ -437,75 +437,26 @@
 able
-Environment;%0Aimport org.springframework.mock.env.MockPropertySource
+ApplicationContext
 ;%0Aim
@@ -585,48 +585,8 @@
 r;%0A%0A
-import javax.annotation.PostConstruct;%0A%0A
 @Run
@@ -653,16 +653,24 @@
 uration(
+value = 
 DemoAppl
@@ -680,30 +680,66 @@
 tion
-Test.TestConfiguration
+.class, initializers = AbstractIntegrationTest.Initializer
 .cla
@@ -957,96 +957,115 @@
     
-@Configuration%0A    @Import(DemoApplication.class)%0A    static class TestConfiguration
+public static class Initializer implements ApplicationContextInitializer%3CConfigurableApplicationContext%3E
  %7B%0A
-%0A
     
@@ -1073,193 +1073,167 @@
    @
-Autowired%0A        ConfigurableEnvironment environment;%0A%0A        @PostConstruct%0A        public void init() %7B%0A            MockPropertySource propertySource = new MockPropertySource(%22TestC
+Override%0A        public void initialize(ConfigurableApplicationContext configurableApplicationContext) %7B%0A            EnvironmentTestUtils.addEnvironment(%22testc
 onta
@@ -1242,51 +1242,79 @@
 ers%22
-);%0A%0A            propertySource.setProperty(
+, configurableApplicationContext.getEnvironment(),%0A                    
 %22spr
@@ -1327,18 +1327,20 @@
 dis.host
-%22,
+=%22 +
  redis.g
@@ -1361,18 +1361,17 @@
 ddress()
-);
+,
 %0A       
@@ -1379,35 +1379,16 @@
     
-propertySource.setProperty(
+        
 %22spr
@@ -1401,18 +1401,20 @@
 dis.port
-%22,
+=%22 +
  redis.g
@@ -1431,19 +1431,16 @@
 rt(6379)
-);%0A
 %0A       
@@ -1448,64 +1448,8 @@
     
-environment.getPropertySources().addFirst(propertySource
 );%0A 

Simplify Spring Boot example