Last active
October 26, 2018 17:51
-
-
Save kh0ma/1dd3ad75d914d1b60b91b64d22a43f10 to your computer and use it in GitHub Desktop.
Parameterized JUnit for SoftServe IT Academy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Math { | |
public static int pow(int number, int exponent) { | |
return (int) java.lang.Math.pow(number, exponent); //just for lazy men ^_^ | |
} | |
public static double mean(double... data) { | |
double sum = 0; | |
int length = data.length; | |
for (int i = 0; i < length; i++) { | |
sum += data[i]; | |
} | |
return sum / length; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert; | |
import org.junit.Test; | |
import org.junit.experimental.runners.Enclosed; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Parameterized; | |
import java.util.Arrays; | |
import java.util.HashSet; | |
/** | |
* @author Oleksandr Khomenko | |
*/ | |
@RunWith(Enclosed.class) //Tell JUnit that "he" have to run static inner tests | |
public class MathTest { | |
@RunWith(Parameterized.class) | |
public static class PowTest { | |
@Parameterized.Parameter | |
public int number; //should be public | |
@Parameterized.Parameter(1) | |
public int exponenta; //should be public | |
@Parameterized.Parameter(2) | |
public int expected; //should be public | |
@Parameterized.Parameters | |
public static Iterable data() { //should be public static | |
Object[] first = {2, 2, 4}; | |
Object[] second = {3, 3, 27}; | |
Object[] third = {3, 13, 1594323}; | |
Object[][] objects = { //an Array with Arrays. Included array should contain as many elements as parameters | |
first, | |
second, | |
third | |
}; | |
return Arrays.asList( //Iterable with Array. Thanks Java varags.... It eat one dimension from array above. Look at method signature of .asList() | |
objects //And we have a List of one dimensional arrays. | |
); | |
} | |
@Test | |
public void pow() { //Can contain more than one test, just add new method below | |
int actual = Math.pow(this.number, this.exponenta); | |
Assert.assertEquals(this.expected, actual); | |
} | |
} | |
@RunWith(Parameterized.class) | |
public static class MeanTest { | |
@Parameterized.Parameter | |
public double a; | |
@Parameterized.Parameter(1) | |
public double b; | |
@Parameterized.Parameter(2) | |
public double c; | |
@Parameterized.Parameter(3) | |
public double e; | |
@Parameterized.Parameter(4) | |
public double f; | |
@Parameterized.Parameter(5) | |
public double x; | |
@Parameterized.Parameter(6) | |
public double expected; | |
@Parameterized.Parameters | |
public static Iterable data() { | |
HashSet<Object> data = new HashSet<>(); | |
data.add(Arrays.asList(1, 1, 1, 1, 1, 1, 1).toArray()); | |
data.add(Arrays.asList(6, 0, 0, 0, 0, 0, 1).toArray()); | |
data.add(Arrays.asList(1, 2, 3, 1, 2, 3, 2).toArray()); | |
return data; | |
} | |
@Test | |
public void mean() { | |
double actual = Math.mean(a,b,c,e,f,x); | |
double delta = 0.01; | |
Assert.assertEquals(this.expected, actual, delta); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment