From 49724d1691e3e7d07fcecd625f91a23be0518947 Mon Sep 17 00:00:00 2001 From: amilabanuka Date: Fri, 26 May 2017 23:47:50 +0800 Subject: [PATCH] BAEL-919: Introduction to JUnitParams (#1938) * BAEL-919 Added the JUnitParams intorduction classes * BAEL-919 Added the JUnitParams intorduction classes * Reverting the adding to libraries folder --- libraries/pom.xml | 1 + testing/pom.xml | 7 +++ .../junitparams/SafeAdditionUtil.java | 15 ++++++ .../junitparams/SafeAdditionUtilTest.java | 54 +++++++++++++++++++ .../junitparams/TestDataProvider.java | 13 +++++ .../resources/JunitParamsTestParameters.csv | 4 ++ 6 files changed, 94 insertions(+) create mode 100644 testing/src/main/java/com/baeldung/junitparams/SafeAdditionUtil.java create mode 100644 testing/src/test/java/com/baeldung/junitparams/SafeAdditionUtilTest.java create mode 100644 testing/src/test/java/com/baeldung/junitparams/TestDataProvider.java create mode 100644 testing/src/test/resources/JunitParamsTestParameters.csv diff --git a/libraries/pom.xml b/libraries/pom.xml index 9c10a13b7b..a4b554365d 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -275,6 +275,7 @@ quartz 2.3.0 + 0.7.0 diff --git a/testing/pom.xml b/testing/pom.xml index a0bc5b99cf..8c6898ac67 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -64,6 +64,12 @@ ${truth.version} test + + pl.pragmatists + JUnitParams + ${jUnitParams.version} + test + @@ -130,5 +136,6 @@ 3.1.0 3.6.1 0.32 + 1.1.0 diff --git a/testing/src/main/java/com/baeldung/junitparams/SafeAdditionUtil.java b/testing/src/main/java/com/baeldung/junitparams/SafeAdditionUtil.java new file mode 100644 index 0000000000..a2c1573dca --- /dev/null +++ b/testing/src/main/java/com/baeldung/junitparams/SafeAdditionUtil.java @@ -0,0 +1,15 @@ +package com.baeldung.junitparams; + +public class SafeAdditionUtil { + + public int safeAdd(int a, int b) { + long result = ((long) a) + b; + if (result > Integer.MAX_VALUE) { + return Integer.MAX_VALUE; + } else if (result < Integer.MIN_VALUE) { + return Integer.MIN_VALUE; + } + return (int) result; + } + +} diff --git a/testing/src/test/java/com/baeldung/junitparams/SafeAdditionUtilTest.java b/testing/src/test/java/com/baeldung/junitparams/SafeAdditionUtilTest.java new file mode 100644 index 0000000000..8ab49309cd --- /dev/null +++ b/testing/src/test/java/com/baeldung/junitparams/SafeAdditionUtilTest.java @@ -0,0 +1,54 @@ +package com.baeldung.junitparams; + +import static org.junit.Assert.assertEquals; + +import junitparams.FileParameters; +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(JUnitParamsRunner.class) +public class SafeAdditionUtilTest { + + private SafeAdditionUtil serviceUnderTest = new SafeAdditionUtil(); + + @Test + @Parameters({ "1, 2, 3", "-10, 30, 20", "15, -5, 10", "-5, -10, -15" }) + public void whenWithAnnotationProvidedParams_thenSafeAdd(int a, int b, int expectedValue) { + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); + } + + @Test + @Parameters(method = "parametersToTestAdd") + public void whenWithNamedMethod_thendSafeAdd(int a, int b, int expectedValue) { + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); + } + + private Object[] parametersToTestAdd() { + 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 } }; + } + + @Test + @Parameters + public void whenWithnoParam_thenLoadByNameSafeAdd(int a, int b, int expectedValue) { + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); + } + + private Object[] parametersForWhenWithnoParam_thenLoadByNameSafeAdd() { + 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 } }; + } + + @Test + @Parameters(source = TestDataProvider.class) + public void whenWithNamedClass_thenSafeAdd(int a, int b, int expectedValue) { + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); + } + + @Test + @FileParameters("src/test/resources/JunitParamsTestParameters.csv") + public void whenWithCsvFile_thenSafeAdd(int a, int b, int expectedValue) { + assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b)); + } + +} diff --git a/testing/src/test/java/com/baeldung/junitparams/TestDataProvider.java b/testing/src/test/java/com/baeldung/junitparams/TestDataProvider.java new file mode 100644 index 0000000000..d318345a56 --- /dev/null +++ b/testing/src/test/java/com/baeldung/junitparams/TestDataProvider.java @@ -0,0 +1,13 @@ +package com.baeldung.junitparams; + +public class TestDataProvider { + + public static Object[] provideBasicData() { + return new Object[] { new Object[] { 1, 2, 3 }, new Object[] { -10, 30, 20 }, new Object[] { 15, -5, 10 }, new Object[] { -5, -10, -15 } }; + } + + public static Object[] provideEdgeCaseData() { + return new Object[] { new Object[] { Integer.MAX_VALUE, 2, Integer.MAX_VALUE }, new Object[] { Integer.MIN_VALUE, -2, Integer.MIN_VALUE }, }; + } + +} diff --git a/testing/src/test/resources/JunitParamsTestParameters.csv b/testing/src/test/resources/JunitParamsTestParameters.csv new file mode 100644 index 0000000000..84eb5a0b23 --- /dev/null +++ b/testing/src/test/resources/JunitParamsTestParameters.csv @@ -0,0 +1,4 @@ +1,2,3 +-10, 30, 20 +15, -5, 10 +-5, -10, -15 \ No newline at end of file