Skip to content

Commit c9cd50d

Browse files
amilabanukapedja4
authored andcommitted
BAEL-919 (eugenp#1931)
Added the JUnitParams intorduction classes
1 parent 7720383 commit c9cd50d

5 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@
269269
<artifactId>h2</artifactId>
270270
<version>1.4.195</version>
271271
</dependency>
272+
<dependency>
273+
<groupId>pl.pragmatists</groupId>
274+
<artifactId>JUnitParams</artifactId>
275+
<version>${jUnitParams.version}</version>
276+
<scope>test</scope>
277+
</dependency>
272278
<dependency>
273279
<groupId>org.quartz-scheduler</groupId>
274280
<artifactId>quartz</artifactId>
@@ -297,6 +303,7 @@
297303
<serenity.jbehave.version>1.24.0</serenity.jbehave.version>
298304
<serenity.jira.version>1.1.3-rc.5</serenity.jira.version>
299305
<serenity.plugin.version>1.4.0</serenity.plugin.version>
306+
<jUnitParams.version>1.1.0</jUnitParams.version>
300307
</properties>
301308

302309
</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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.junitparams;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
8+
import junitparams.FileParameters;
9+
import junitparams.JUnitParamsRunner;
10+
import junitparams.Parameters;
11+
12+
@RunWith(JUnitParamsRunner.class)
13+
public class SafeAdditionUtilTest {
14+
15+
private SafeAdditionUtil serviceUnderTest = new SafeAdditionUtil();
16+
17+
@Test
18+
@Parameters({ "1, 2, 3", "-10, 30, 20", "15, -5, 10", "-5, -10, -15" })
19+
public void whenCalledWithAnnotationProvidedParams_thenSafeAddAndReturn(int a, int b, int expectedValue) {
20+
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
21+
}
22+
23+
@Test
24+
@Parameters(method = "parametersToTestAdd")
25+
public void whenCalledWithNamedMethod_thendSafeAddAndReturn(int a, int b, int expectedValue) {
26+
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
27+
}
28+
29+
private Object[] parametersToTestAdd() {
30+
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 } };
31+
}
32+
33+
@Test
34+
@Parameters
35+
public void whenCalledWithnoParam_thenLoadByNameSafeAddAndReturn(int a, int b, int expectedValue) {
36+
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
37+
}
38+
39+
private Object[] parametersForWhenCalledWithnoParam_thenLoadByNameSafeAddAndReturn() {
40+
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 } };
41+
}
42+
43+
@Test
44+
@Parameters(source = TestDataProvider.class)
45+
public void whenCalledWithNamedClass_thenSafeAddAndReturn(int a, int b, int expectedValue) {
46+
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
47+
}
48+
49+
@Test
50+
@FileParameters("src/test/resources/JunitParamsTestParameters.csv")
51+
public void whenCalledWithCsvFile_thenSafeAddAndReturn(int a, int b, int expectedValue) {
52+
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
53+
}
54+
55+
}
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)