Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

junit5 MethodSource in nested class

Tags:

java

junit

junit5

I am working with junit5 and I want to create Parameterized Test in nested class. For example:

class CardTest {      @Nested     class Cost {         Stream<Arguments> cards() {             return Stream.of(                     Arguments.of(Card.common(0, Color.RED), 0),                     /** Other Data **/                     Arguments.of(Card.choseColor(), 50)             );         }          @MethodSource("cards")         @ParameterizedTest         void cardCost(Card card, int cost) {             assertThat(card.cost())                     .isEqualTo(cost);         }     }     /** Some other nested classes or simple methods **/ } 

The problem is @MethodSource required that specified method must be static. But Java not allowed static method in non-static inner classes. If I create class Cost static then it won't be collected by junit.

What should I do to resolve this issue?

like image 550
eugene-karanda Avatar asked Dec 21 '17 22:12

eugene-karanda


1 Answers

@TestInstance(PER_CLASS)

You may select the "single test instance per class" mode annotating the nested class with @TestInstance(TestInstance.Lifecycle.PER_CLASS):

class ColorTest {      @Nested     @TestInstance(TestInstance.Lifecycle.PER_CLASS)     class Inner {          @ParameterizedTest         @MethodSource("colors")         void blue(Color color, int blue) {             Assertions.assertEquals(color.getBlue(), blue);         }          Stream<Arguments> colors() {             return Stream.of(                     Arguments.of(Color.BLACK, 0),                     Arguments.of(Color.GRAY, 128),                     Arguments.of(Color.BLUE, 255)             );         }     }  } 

When using this mode, a new test instance will be created once per test class.

ArgumentsProvider

Or you may switch from a MethodSource to an ArgumentsProvider.

I modified your example to see if it compiles and runs locally:

class ColorTest {      static class Blues implements ArgumentsProvider {          @Override         public Stream<Arguments> provideArguments(ExtensionContext context) {             return Stream.of(                     Arguments.of(Color.BLACK, 0),                     Arguments.of(Color.GRAY, 128),                     Arguments.of(Color.BLUE, 255)             );         }     }      @Nested     class Inner {          @ParameterizedTest         @ArgumentsSource(Blues.class)         void blue(Color color, int blue) {             Assertions.assertEquals(color.getBlue(), blue);         }     }  } 

More details at http://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

like image 51
Sormuras Avatar answered Sep 19 '22 14:09

Sormuras