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
This commit is contained in:
parent
8e709d587d
commit
49724d1691
|
@ -275,6 +275,7 @@
|
||||||
<artifactId>quartz</artifactId>
|
<artifactId>quartz</artifactId>
|
||||||
<version>2.3.0</version>
|
<version>2.3.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<properties>
|
<properties>
|
||||||
<multiverse.version>0.7.0</multiverse.version>
|
<multiverse.version>0.7.0</multiverse.version>
|
||||||
|
|
|
@ -64,6 +64,12 @@
|
||||||
<version>${truth.version}</version>
|
<version>${truth.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>pl.pragmatists</groupId>
|
||||||
|
<artifactId>JUnitParams</artifactId>
|
||||||
|
<version>${jUnitParams.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -130,5 +136,6 @@
|
||||||
<assertj-guava.version>3.1.0</assertj-guava.version>
|
<assertj-guava.version>3.1.0</assertj-guava.version>
|
||||||
<assertj-core.version>3.6.1</assertj-core.version>
|
<assertj-core.version>3.6.1</assertj-core.version>
|
||||||
<truth.version>0.32</truth.version>
|
<truth.version>0.32</truth.version>
|
||||||
|
<jUnitParams.version>1.1.0</jUnitParams.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 }, };
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
1,2,3
|
||||||
|
-10, 30, 20
|
||||||
|
15, -5, 10
|
||||||
|
-5, -10, -15
|
|
Loading…
Reference in New Issue