0

Starting from the following generic code, showing a sample test class that needs to be executed for several rounds of input data:

import java.util.stream.Stream;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DummyManyTests {

    @BeforeAll
    public void beforeAll() {
        log.info("before all...");
    }

    @AfterAll
    public void afterAll() {
        log.info("after all...");
    }

    static Stream<Integer> countParameterProvider() {
        return Stream.of(50, 100, 200);
    }

    @ParameterizedTest
    @MethodSource("countParameterProvider")
    @Order(10)
    public void m1(int count) {
        log.info("m1 #count: {}", count);
    }

    @ParameterizedTest
    @MethodSource("countParameterProvider")
    @Order(20)
    public void m2(int count) {
        log.info("m2 #count: {}", count);
    }

    @ParameterizedTest
    @MethodSource("countParameterProvider")
    @Order(30)
    public void m3(int count) {
        log.info("m3 #count: {}", count);
    }

}

Which generates this output:

15:38:54.878 [main] INFO com...DummyManyTests - before all...
15:38:54.973 [main] INFO com...DummyManyTests - m1 #count: 50
15:38:55.004 [main] INFO com...DummyManyTests - m1 #count: 100
15:38:55.007 [main] INFO com...DummyManyTests - m1 #count: 200
15:38:55.013 [main] INFO com...DummyManyTests - m2 #count: 50
15:38:55.015 [main] INFO com...DummyManyTests - m2 #count: 100
15:38:55.018 [main] INFO com...DummyManyTests - m2 #count: 200
15:38:55.021 [main] INFO com...DummyManyTests - m3 #count: 50
15:38:55.025 [main] INFO com...DummyManyTests - m3 #count: 100
15:38:55.028 [main] INFO com...DummyManyTests - m3 #count: 200
15:38:55.030 [main] INFO com...DummyManyTests - after all...

I need to change the way the tests are parameterized.
Basically I need to have the entire entire class executed end-to-end (including beforeAll and afterAll), for each value of that parameter.

The output would then become:

15:38:54.878 [main] INFO com...DummyManyTests - before all...
15:38:54.973 [main] INFO com...DummyManyTests - m1 #count: 50
15:38:55.013 [main] INFO com...DummyManyTests - m2 #count: 50
15:38:55.021 [main] INFO com...DummyManyTests - m3 #count: 50
15:38:55.030 [main] INFO com...DummyManyTests - after all...
15:38:54.878 [main] INFO com...DummyManyTests - before all...
15:38:55.004 [main] INFO com...DummyManyTests - m1 #count: 100
15:38:55.015 [main] INFO com...DummyManyTests - m2 #count: 100
15:38:55.025 [main] INFO com...DummyManyTests - m3 #count: 100
15:38:55.030 [main] INFO com...DummyManyTests - after all...
15:38:54.878 [main] INFO com...DummyManyTests - before all...
15:38:55.007 [main] INFO com...DummyManyTests - m1 #count: 200
15:38:55.018 [main] INFO com...DummyManyTests - m2 #count: 200
15:38:55.028 [main] INFO com...DummyManyTests - m3 #count: 200
15:38:55.030 [main] INFO com...DummyManyTests - after all...

How can I achieve this ?

6
  • 1
    There's no way to parametrize class See this answer: stackoverflow.com/questions/46182118/… Commented Jan 16, 2024 at 14:58
  • 1
    I've done what's suggested in stackoverflow.com/a/77008774/1180351 a few times, works pretty well Commented Jan 16, 2024 at 16:54
  • Deriving from a superclass or interface is the only supported way to parameterized a full test suite. If that‘s suitable for your case depends on how many variations you have. Half a dozen is probably still reasonable. A few hundred subclasses probably not. Commented Jan 16, 2024 at 19:04
  • @RobSpoor that seems to be the closest thing to what I need, at least from what I could find so far... Commented Jan 17, 2024 at 9:09
  • 1
    @johanneslink if you have that many, or need dynamic parameterization, you can always go for @TestFactory and DynamicContainer. Whenever I need to do that, I usually create a private method for creating each nested DynamicContainer and DynamicTest so the code won't become unreadable. Commented Jan 17, 2024 at 9:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.