4

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));
    }

}

1 Answers1

2
@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);
eee
  • 3,241
  • 1
  • 17
  • 34
  • 1
    can parametrised name be formatted in nicer way instead of just index? issue is here how to format (aka override toString()) of first parameter which is int[][] object – Ewoks Jul 12 '17 at 19:29