|
| 1 | +package com.baeldung.junitparams; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import junitparams.FileParameters; |
| 6 | +import junitparams.JUnitParamsRunner; |
| 7 | +import junitparams.Parameters; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | + |
| 11 | +@RunWith(JUnitParamsRunner.class) |
| 12 | +public class SafeAdditionUtilTest { |
| 13 | + |
| 14 | + private SafeAdditionUtil serviceUnderTest = new SafeAdditionUtil(); |
| 15 | + |
| 16 | + @Test |
| 17 | + @Parameters({ "1, 2, 3", "-10, 30, 20", "15, -5, 10", "-5, -10, -15" }) |
| 18 | + public void whenWithAnnotationProvidedParams_thenSafeAdd(int a, int b, int expectedValue) { |
| 19 | + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + @Parameters(method = "parametersToTestAdd") |
| 24 | + public void whenWithNamedMethod_thendSafeAdd(int a, int b, int expectedValue) { |
| 25 | + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); |
| 26 | + } |
| 27 | + |
| 28 | + private Object[] parametersToTestAdd() { |
| 29 | + return new Object[] { new Object[] { 1, 2, 3 }, new Object[] { -10, 30, 20 }, new Object[] { Integer.MAX_VALUE, 2, Integer.MAX_VALUE }, new Object[] { Integer.MIN_VALUE, -8, Integer.MIN_VALUE } }; |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @Parameters |
| 34 | + public void whenWithnoParam_thenLoadByNameSafeAdd(int a, int b, int expectedValue) { |
| 35 | + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); |
| 36 | + } |
| 37 | + |
| 38 | + private Object[] parametersForWhenWithnoParam_thenLoadByNameSafeAdd() { |
| 39 | + return new Object[] { new Object[] { 1, 2, 3 }, new Object[] { -10, 30, 20 }, new Object[] { Integer.MAX_VALUE, 2, Integer.MAX_VALUE }, new Object[] { Integer.MIN_VALUE, -8, Integer.MIN_VALUE } }; |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + @Parameters(source = TestDataProvider.class) |
| 44 | + public void whenWithNamedClass_thenSafeAdd(int a, int b, int expectedValue) { |
| 45 | + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @FileParameters("src/test/resources/JunitParamsTestParameters.csv") |
| 50 | + public void whenWithCsvFile_thenSafeAdd(int a, int b, int expectedValue) { |
| 51 | + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments