简体   繁体   中英

JUnit 5 with Spring boot - how repeat test with different profiles?

Here is classical example of the JUnit 5 data-driven test.

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
class ScrathTest {
  @Autowired
  private MyBean myBean;

  @ParameterizedTest
  @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) // six numbers
  void isOdd_ShouldReturnTrueForOddNumbers(int number) {
    myBean.doSomeThing(number)
  }
}

But what if I need to run this not for integer array, but profiles array? I mean I create a single test, and 3 test profiles then repeat this test 3 times with different profiles in each innovation. Is it possible?

Note

@ActiveProfile annotation is not a solution because it simply activates listed profiles without test repeating and context recreation.

I don't think that JUnit5 provides a similar feature. But, you can create an abstract class and derived class will specify the active profile.

class abstract AbstactScrathTest {

  @Autowired
  protected MyBean myBean;

  @ParameterizedTest
  @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) // six numbers
  void isOdd_ShouldReturnTrueForOddNumbers(int number) {
    myBean.doSomeThing(number)
  }

}

@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test1")
class ScrathTestWithTestProfile1 extends AbstractScrathTest{
}


@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test2")
class ScrathTestWithTestProfile2 extends AbstractScrathTest{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM