Skip to content

Commit 49724d1

Browse files
amilabanukapedja4
authored andcommitted
BAEL-919: Introduction to JUnitParams (eugenp#1938)
* BAEL-919 Added the JUnitParams intorduction classes * BAEL-919 Added the JUnitParams intorduction classes * Reverting the adding to libraries folder
1 parent 8e709d5 commit 49724d1

6 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
<artifactId>quartz</artifactId>
276276
<version>2.3.0</version>
277277
</dependency>
278+
278279
</dependencies>
279280
<properties>
280281
<multiverse.version>0.7.0</multiverse.version>

‎testing/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
<version>${truth.version}</version>
6565
<scope>test</scope>
6666
</dependency>
67+
<dependency>
68+
<groupId>pl.pragmatists</groupId>
69+
<artifactId>JUnitParams</artifactId>
70+
<version>${jUnitParams.version}</version>
71+
<scope>test</scope>
72+
</dependency>
6773
</dependencies>
6874
<build>
6975
<plugins>
@@ -130,5 +136,6 @@
130136
<assertj-guava.version>3.1.0</assertj-guava.version>
131137
<assertj-core.version>3.6.1</assertj-core.version>
132138
<truth.version>0.32</truth.version>
139+
<jUnitParams.version>1.1.0</jUnitParams.version>
133140
</properties>
134141
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.junitparams;
2+
3+
public class SafeAdditionUtil {
4+
5+
public int safeAdd(int a, int b) {
6+
long result = ((long) a) + b;
7+
if (result > Integer.MAX_VALUE) {
8+
return Integer.MAX_VALUE;
9+
} else if (result < Integer.MIN_VALUE) {
10+
return Integer.MIN_VALUE;
11+
}
12+
return (int) result;
13+
}
14+
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.junitparams;
2+
3+
public class TestDataProvider {
4+
5+
public static Object[] provideBasicData() {
6+
return new Object[] { new Object[] { 1, 2, 3 }, new Object[] { -10, 30, 20 }, new Object[] { 15, -5, 10 }, new Object[] { -5, -10, -15 } };
7+
}
8+
9+
public static Object[] provideEdgeCaseData() {
10+
return new Object[] { new Object[] { Integer.MAX_VALUE, 2, Integer.MAX_VALUE }, new Object[] { Integer.MIN_VALUE, -2, Integer.MIN_VALUE }, };
11+
}
12+
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1,2,3
2+
-10, 30, 20
3+
15, -5, 10
4+
-5, -10, -15

0 commit comments

Comments
 (0)