I am having a problem to parameterized with JUnit 4.x. My parameterized test consists of 1 array mixed type {{integer multidimensional array} and 1 double} as parameter and I am having difficulty of how to declare them. See code below.
Class to Test Robot
public class Robot {
public static double companyBotStrategy(int[][] trainingData) {
double botTime = 0;
double isCorrect = 0;
for (int i = 0; i < trainingData.length; i++) {
int[] v = trainingData[i];
if (v[1] == 1) {
botTime += v[0];
isCorrect++;
}
}
return botTime / isCorrect;
}
}
JUnit Test Parameterized
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;
public class RobotPrmtTest {
private double expected;
private int[][] trainingData;
public RobotPrmtTest(int[][] trainingData, double expected) {
this.trainingData = trainingData;
this.expected = expected;
}
@Parameters(name = "{index}")
public static Iterable<Object[]> getLoadTest() {
return Arrays.asList(new Object[][] { });
/*loadTest array mix type
* {int [][] trainingData, double expected}
* looks like it
{
{
{{ 6, 1 }, { 4, 1 }},4.5
},
{
{{4,1},{4,-1}, {0,0}, {6,1}},5.0
}
}
*/
}
@Test
public void validateParamaters() {
assertEquals("divergente", expected, Robot.companyBotStrategy(trainingData));
}
}
@Parameters(name = "{index}")
public static Iterable<Object[]> getLoadTest() {
return Arrays.asList(new Object[][] {
{
new int[][]{{6, 1}, {4, 1}}, 4.5
},
{
new int[][]{{4, 1}, {4, -1}, {0, 0}, {6, 1}}, 5.0
}
});
}
Side note : You have to supply a delta in your assert method:
double delta = 0.1; // choose something appropriate here
assertEquals("divergente", expected, Robot.companyBotStrategy(trainingData), delta);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.