JUnit Test Runner

  

What is a JUnit Test Runner?

JUnit Test Runner is the component in JUnit responsible for executing test cases. It defines how tests are discovered, initialized, and run. JUnit provides built-in runners, but you can also create custom runners.


Common JUnit Test Runners


Test RunnerDescription
BlockJUnit4ClassRunnerDefault runner for JUnit 4. Runs tests in a class using annotations like @Test, @Before, @After.
ParameterizedAllows running the same test with different inputs.
SuiteRuns multiple test classes together. Useful for integration testing.
SpringJUnit4ClassRunnerUsed in Spring testing to load the Spring context.



How to Use a Test Runner in JUnit

You attach a runner using the @RunWith annotation on top of your test class:


@RunWith(BlockJUnit4ClassRunner.class)
public class MyTestClass {
    // Your test methods
}


However, if you're using basic JUnit 4 tests, you don’t need to specify @RunWith, because BlockJUnit4ClassRunner is the default.


Example 1: Using Default Runner (JUnit 4)


import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CalculatorTest {

    @Test
    public void testAddition() {
        int result = 2 + 3;
        assertEquals(5, result);
    }

    @Test
    public void testSubtraction() {
        int result = 5 - 2;
        assertEquals(3, result);
    }
}




Here, JUnit uses the default BlockJUnit4ClassRunner implicitly.

Example 2: Using @RunWith(Suite.class) to Run Multiple Classes


import org.junit.runner.RunWith;
import org.junit.runners.Suite;

// List the test classes
@RunWith(Suite.class)
@Suite.SuiteClasses({
    CalculatorTest.class,
    StringUtilsTest.class
})
public class AllTests {
    // No code needed here
}



This AllTests class acts as a test suite runner.

Example 3: Using @RunWith(Parameterized.class) for Data-Driven Testing


import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class MultiplyTest {

    private int a, b, expected;

    public MultiplyTest(int a, int b, int expected) {
        this.a = a;
        this.b = b;
        this.expected = expected;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> testData() {
        return Arrays.asList(new Object[][] {
            {2, 3, 6},
            {4, 5, 20},
            {3, 3, 9}
        });
    }

    @Test
    public void testMultiplication() {
        assertEquals(expected, a * b);
    }
}


This uses Parameterized runner to run the test method multiple times with different input sets.

What is a Test Runner in JUnit 5?

In JUnit 5, the concept of a Test Runner is abstracted away — it's handled internally by the JUnit Platform.

You don't explicitly use @RunWith anymore. Instead, you rely on annotations like @Test@BeforeEach@AfterEach, and engines like:

  • junit-jupiter-engine – runs JUnit 5 tests

  • junit-vintage-engine – runs legacy JUnit 3 & 4 tests


JUnit 5 integrates well with tools like Maven, Gradle, Eclipse, IntelliJ, and CI tools, and the runner is embedded in these integrations.


JUnit 5 Parameterized Test Example

In JUnit 5, you use the @ParameterizedTest annotation with sources like @ValueSource@CsvSource, etc.


import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class NumberTest {

    @ParameterizedTest
    @ValueSource(ints = {2, 4, 6, 8})
    void testEvenNumbers(int number) {
        assertTrue(number % 2 == 0);
    }
}

This is similar to @RunWith(Parameterized.class) in JUnit 4.

No comments:

Post a Comment