Spring Download / JUnit, run all unit tests for multiple profiles

I have a BaseTest class that consists of several tests. Each test must be performed for EACH profile I. List

I was thinking about using parameterized values ​​such as:

@RunWith(Parameterized.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// @ActiveProfiles("h2-test") // <-- how to iterate over this?
public abstract class BaseTest {

@Autowired
private TestRepository test;

// to be used with Parameterized/Spring
private TestContextManager testContextManager;

public BaseTest(String profile) {
   System.setProperty("spring.profiles.active", profile);
   // TODO what now?
}

@Parameterized.Parameters
public static Collection<Object[]> data() {
  Collection<Object[]> params = new ArrayList<>();
  params.add(new Object[] {"h2-test" });
  params.add(new Object[] {"mysql-test" });
  return params;
}

@Before 
public void setUp() throws Exception {
  this.testContextManager = new TestContextManager(getClass());
  this.testContextManager.prepareTestInstance(this);
  // maybe I can spinup Spring here with my profile?
}

@Test
public void testRepository() {
  Assert.assertTrue(test.exists("foo"))
}

How can I tell Spring to run each test with these different profiles? In fact, each profile will talk to different data sources (inside h2 memory, external mysql, external oracle, ..), so my repository / data source needs to be reinitialized.


, @ActiveProfiles (...), BaseTest ActiveProfile. , . BaseTest, . , :

  • BaseTest (@ActiveProfiles ( "mysql" ))
    • FooClassMySQL ( BaseTest)
      • FooClassH2 (@ActiveProfiles ( "h2" ))
    • BarClassMySQL ( BaseTest)
      • BarClassH2 (@ActiveProfiles ( "h2" ))

+6
2

Maven, (, , env):

mvn clean test -Dspring.profiles.active=h2-test

, , Spring . , , . , JUnit ( ).

EDIT: - @RunWith(Parameterized.class), . ( , ), runner, SpringRunner.class - , .

+3

Spring .
.
Spring Boot .

, , , Maven Spring , .

, env .
, Spring , .

@ActiveProfiles.

, , ( ) Maven, Spring :

mvn clean test -Dspring.profiles.active=h2

mvn clean test -Dspring.profiles.active=mysql

....

, script, maven.
, , , .

+4

All Articles