What is a JUnit Test Runner?
A 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 Runner | Description |
|---|---|
| BlockJUnit4ClassRunner | Default runner for JUnit 4. Runs tests in a class using annotations like @Test, @Before, @After. |
| Parameterized | Allows running the same test with different inputs. |
| Suite | Runs multiple test classes together. Useful for integration testing. |
| SpringJUnit4ClassRunner | Used in Spring testing to load the Spring context. |
@RunWith annotation on top of your test class:@RunWith(BlockJUnit4ClassRunner.class) public class MyTestClass { // Your test methods }
@RunWith, because BlockJUnit4ClassRunner is the default.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); } }
BlockJUnit4ClassRunner implicitly.@RunWith(Suite.class) to Run Multiple Classesimport 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 }
AllTests class acts as a test suite runner.@RunWith(Parameterized.class) for Data-Driven Testingimport 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); } }
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 testsjunit-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); } }
@RunWith(Parameterized.class) in JUnit 4.
No comments:
Post a Comment