Last active
January 27, 2025 05:23
-
-
Save jeantessier/13b06bf134530db48d263bd19b52e7c2 to your computer and use it in GitHub Desktop.
Data-Driven tests in JUnit 4 and JUnit Jupiter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SomeClassUnderTest { | |
| public String someMethodUnderTest(Object input) { | |
| return switch (input) { | |
| case Number number -> String.format("%.2f", number.doubleValue()); | |
| case String string -> String.format("%.2f", Double.parseDouble(string)); | |
| case null -> "null"; | |
| default -> input.toString(); | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.Test; | |
| public class TestSomeClassUnderTest_JUnit4_withInvalidInput { | |
| private final SomeClassUnderTest sut = new SomeClassUnderTest(); | |
| @Test(expected=Exception.class) | |
| public void test() { | |
| sut.someMethodUnderTest("invalid"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.junit.runners.Parameterized; | |
| import static org.junit.Assert.assertEquals; | |
| import static org.junit.runners.Parameterized.Parameter; | |
| import static org.junit.runners.Parameterized.Parameters; | |
| @RunWith(Parameterized.class) | |
| public class TestSomeClassUnderTest_JUnit4_withValidInputs { | |
| @Parameters(name="when the input is {0}") | |
| public static Object[][] data() { | |
| return new Object[][] { | |
| {"a string", "123", "123.00"}, | |
| {"an integer", 123, "123.00"}, | |
| {"a float", 123.0, "123.00"}, | |
| {"a negative number", -123, "-123.00"}, | |
| {"null", null, "null"}, | |
| }; | |
| } | |
| @Parameter(0) | |
| public String variation; | |
| @Parameter(1) | |
| public Object input; | |
| @Parameter(2) | |
| public String expectedOutput; | |
| private final SomeClassUnderTest sut = new SomeClassUnderTest(); | |
| @Test | |
| public void test() { | |
| var actualOutput = sut.someMethodUnderTest(input); | |
| assertEquals(variation, expectedOutput, actualOutput); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.stream.Stream; | |
| import org.junit.jupiter.api.DisplayName; | |
| import org.junit.jupiter.api.Test; | |
| import org.junit.jupiter.params.ParameterizedTest; | |
| import org.junit.jupiter.params.provider.Arguments; | |
| import org.junit.jupiter.params.provider.MethodSource; | |
| import static org.junit.jupiter.api.Assertions.assertEquals; | |
| import static org.junit.jupiter.api.Assertions.assertThrows; | |
| import static org.junit.jupiter.params.provider.Arguments.arguments; | |
| class TestSomeClassUnderTest_JUnit5 { | |
| static Stream<Arguments> dataProvider() { | |
| return Stream.of( | |
| arguments("a string", "123", "123.00"), | |
| arguments("an integer", 123, "123.00"), | |
| arguments("a float", 123.0, "123.00"), | |
| arguments("a negative number", -123, "-123.00"), | |
| arguments("a null", null, "null") | |
| ); | |
| } | |
| private final SomeClassUnderTest sut = new SomeClassUnderTest(); | |
| @DisplayName("valid inputs") | |
| @ParameterizedTest(name = "when the input is {0} {1} should be ''{2}''") | |
| @MethodSource("dataProvider") | |
| void validInputs(String variation, Object input, String expectedOutput) { | |
| var actualOutput = sut.someMethodUnderTest(input); | |
| assertEquals(expectedOutput, actualOutput); | |
| } | |
| @Test | |
| void invalidInput() { | |
| assertThrows(Exception.class, () -> sut.someMethodUnderTest("invalid")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment