JUnit Pioneer RangeSource is an implementation of the ArgumentsSource interface
. It can be used to provide a series of numbers as an input to a @ParameterizedTest
annotation.
ByteRangeSource
DoubleRangeSource
FloatRangeSource
IntRangeSource
LongRangeSource
ShortRangeSource
package com.example.junit5.sujan;
public class AppleCalculator {
byte isValidByte(byte digit) {
Byte aByte = Byte.valueOf(digit);
return aByte;
}
short isValidShort(short digit) {
Short aShort = Short.valueOf(digit);
return aShort;
}
int isValidInt(int digit) {
Integer integer = Integer.valueOf(digit);
return integer;
}
long isValidLong(long digit) {
Long aLong = Long.valueOf(digit);
return aLong;
}
float isValidFlot(float digit) {
Float aFloat = Float.valueOf(digit);
return aFloat;
}
double isValidDouble(double digit) {
Double aDouble = Double.valueOf(digit);
return aDouble;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.params.ParameterizedTest;
import org.junitpioneer.jupiter.params.ByteRangeSource;
import org.junitpioneer.jupiter.params.DoubleRangeSource;
import org.junitpioneer.jupiter.params.FloatRangeSource;
import org.junitpioneer.jupiter.params.IntRangeSource;
import org.junitpioneer.jupiter.params.LongRangeSource;
import org.junitpioneer.jupiter.params.ShortRangeSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@ParameterizedTest
@ByteRangeSource(from = 0, to = 10)
void ByteRangeTest(byte digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidByte(digit));
}
@ParameterizedTest
@ShortRangeSource(from = 0, to = 10)
void shortRangeTest(short digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidShort(digit));
}
@ParameterizedTest
@IntRangeSource(from = 0, to = 10)
void intRangeTest(int digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidInt(digit));
}
@ParameterizedTest
@LongRangeSource(from = 0, to = 10)
void longRangeTest(long digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidLong(digit));
}
@ParameterizedTest
@FloatRangeSource(from = 0, to = 10)
void floatRangeTest(float digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidFlot(digit));
}
@ParameterizedTest
@DoubleRangeSource(from = 0, to = 10)
void doubleRangeTest(double digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidDouble(digit));
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}
JUnit Pioneer RangeSource can be negative also.
package com.example.junit5.sujan;
public class AppleCalculator {
byte isValidByte(byte digit) {
Byte aByte = Byte.valueOf(digit);
return aByte;
}
short isValidShort(short digit) {
Short aShort = Short.valueOf(digit);
return aShort;
}
int isValidInt(int digit) {
Integer integer = Integer.valueOf(digit);
return integer;
}
long isValidLong(long digit) {
Long aLong = Long.valueOf(digit);
return aLong;
}
float isValidFlot(float digit) {
Float aFloat = Float.valueOf(digit);
return aFloat;
}
double isValidDouble(double digit) {
Double aDouble = Double.valueOf(digit);
return aDouble;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.params.ParameterizedTest;
import org.junitpioneer.jupiter.params.ByteRangeSource;
import org.junitpioneer.jupiter.params.DoubleRangeSource;
import org.junitpioneer.jupiter.params.FloatRangeSource;
import org.junitpioneer.jupiter.params.IntRangeSource;
import org.junitpioneer.jupiter.params.LongRangeSource;
import org.junitpioneer.jupiter.params.ShortRangeSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@ParameterizedTest
@ByteRangeSource(from =-10, to = 10)
void ByteRangeTest(byte digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidByte(digit));
}
@ParameterizedTest
@ShortRangeSource(from =-10, to = 10)
void shortRangeTest(short digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidShort(digit));
}
@ParameterizedTest
@IntRangeSource(from =-10, to = 10)
void intRangeTest(int digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidInt(digit));
}
@ParameterizedTest
@LongRangeSource(from =-10, to = 10)
void longRangeTest(long digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidLong(digit));
}
@ParameterizedTest
@FloatRangeSource(from =-10, to = 10)
void floatRangeTest(float digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidFlot(digit));
}
@ParameterizedTest
@DoubleRangeSource(from =-10, to = 10)
void doubleRangeTest(double digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidDouble(digit));
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}
JUnit Pioneer RangeSource provides us a way to provide input range to @ParameterizedTest
start and end. we can control the difference between the consecutive values.
package com.example.junit5.sujan;
public class AppleCalculator {
byte isValidByte(byte digit) {
Byte aByte = Byte.valueOf(digit);
return aByte;
}
short isValidShort(short digit) {
Short aShort = Short.valueOf(digit);
return aShort;
}
int isValidInt(int digit) {
Integer integer = Integer.valueOf(digit);
return integer;
}
long isValidLong(long digit) {
Long aLong = Long.valueOf(digit);
return aLong;
}
float isValidFlot(float digit) {
Float aFloat = Float.valueOf(digit);
return aFloat;
}
double isValidDouble(double digit) {
Double aDouble = Double.valueOf(digit);
return aDouble;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.params.ParameterizedTest;
import org.junitpioneer.jupiter.params.ByteRangeSource;
import org.junitpioneer.jupiter.params.DoubleRangeSource;
import org.junitpioneer.jupiter.params.FloatRangeSource;
import org.junitpioneer.jupiter.params.IntRangeSource;
import org.junitpioneer.jupiter.params.LongRangeSource;
import org.junitpioneer.jupiter.params.ShortRangeSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@ParameterizedTest
@ByteRangeSource(from = -10, to = 10, step = 1)
void ByteRangeTest(byte digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidByte(digit));
}
@ParameterizedTest
@ShortRangeSource(from = -10, to = 10, step = 1)
void shortRangeTest(short digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidShort(digit));
}
@ParameterizedTest
@IntRangeSource(from = -10, to = 10, step = 1)
void intRangeTest(int digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidInt(digit));
}
@ParameterizedTest
@LongRangeSource(from = -10, to = 10, step = 1)
void longRangeTest(long digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidLong(digit));
}
@ParameterizedTest
@FloatRangeSource(from = -10, to = 10, step = 1)
void floatRangeTest(float digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidFlot(digit));
}
@ParameterizedTest
@DoubleRangeSource(from = -10, to = 10, step = 1)
void doubleRangeTest(double digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidDouble(digit));
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}
By default RangeSource
execute inclusive of from and to both, but we can change this behaviour. argument closed
decide argument to
will be inclusive or not.
package com.example.junit5.sujan;
public class AppleCalculator {
byte isValidByte(byte digit) {
Byte aByte = Byte.valueOf(digit);
return aByte;
}
short isValidShort(short digit) {
Short aShort = Short.valueOf(digit);
return aShort;
}
int isValidInt(int digit) {
Integer integer = Integer.valueOf(digit);
return integer;
}
long isValidLong(long digit) {
Long aLong = Long.valueOf(digit);
return aLong;
}
float isValidFlot(float digit) {
Float aFloat = Float.valueOf(digit);
return aFloat;
}
double isValidDouble(double digit) {
Double aDouble = Double.valueOf(digit);
return aDouble;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.params.ParameterizedTest;
import org.junitpioneer.jupiter.params.ByteRangeSource;
import org.junitpioneer.jupiter.params.DoubleRangeSource;
import org.junitpioneer.jupiter.params.FloatRangeSource;
import org.junitpioneer.jupiter.params.IntRangeSource;
import org.junitpioneer.jupiter.params.LongRangeSource;
import org.junitpioneer.jupiter.params.ShortRangeSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@ParameterizedTest
@ByteRangeSource(from = 0, to = 1, closed = true)
void ByteRangeTest(byte digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidByte(digit));
}
@ParameterizedTest
@ShortRangeSource(from = 0, to = 1, closed = true)
void shortRangeTest(short digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidShort(digit));
}
@ParameterizedTest
@IntRangeSource(from = 0, to = 1, closed = true)
void intRangeTest(int digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidInt(digit));
}
@ParameterizedTest
@LongRangeSource(from = 0, to = 1, closed = true)
void longRangeTest(long digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidLong(digit));
}
@ParameterizedTest
@FloatRangeSource(from = 0, to = 1, closed = true)
void floatRangeTest(float digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidFlot(digit));
}
@ParameterizedTest
@DoubleRangeSource(from = 0, to = 1, closed = true)
void doubleRangeTest(double digit) {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(digit, appleCalculator.isValidDouble(digit));
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}