简体   繁体   中英

JUnit 5 parameterized test: Using CSVFileSource with Enclosed.Class

I am trying to run parameterized and non parameterized test cases in the same class using

@ExtendWith(MockitoExtension.class)
@RunWith(Enclosed.class)

But somehow tests are not running. I have tried the csvFileSource without Enclosed class and it works fine. This is how my test class skeleton looks like: (Please help)

@ExtendWith(MockitoExtension.class)
@RunWith(Enclosed.class)
public class MyTest {
   static class Base{
   }

   @RunWith(Parameterized.class)
   public static class ParameterizedTests extends Base {
       @ParameterizedTest(name = "testString:{0}")
       @CsvFileSource(resources = "testCases.csv")
       public void test(String testString) {
          ....
       }
   }
}

First, @RunWith is an annotation from JUnit 4. You don't need it when running JUnit Jupiter's @ParameterizedTest . See https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests for details.

Next, only non-static nested classes (ie inner classes) can serve as @Nested test classes. See https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested for details and reasoning.

Minimal working example:

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class MyTest {

  class Base {}

  @Nested
  class ParameterizedTests extends Base {
    @ParameterizedTest(name = "testString:{0}")
    @ValueSource(strings = "testCases.csv")
    void test(String testString) {
      System.out.println(testString);
    }
  }
}

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