Question

I'm having a problem with parameterized testing in Junit. I have been stuck on this for a while now and I was wondering if someone could help me out.

Here is my code

@RunWith(Parameterized.class)
public class DomainTestWithinBorders {

int x;
float y;
boolean expectedOut;

void DomainTest(int xIn, int yIn, boolean out) {
    this.x = xIn;
    this.y = yIn;
    expectedOut = out;
}

@Test
public void testEqual() {
    boolean actualOut = (x == y);
    assertEquals(expectedOut, actualOut);
}

@Parameters
public static Collection<Object[]> data() {
    Object[][] values = { { 0, 10.0, false }, { 1, 16.0, false },
            { 17, 17.0, true } };
    return Arrays.asList(values);
}
}

When running this I get the following error:

java.lang.IllegalArgumentException: wrong number of arguments

I have no clue why I get this error. I feel like i've tried everything.

Was it helpful?

Solution

First of all, your constructor is not really a constructor:

void DomainTest(int xIn, int yIn, boolean out) {
    this.x = xIn;
    this.y = yIn;
    expectedOut = out;
}

should be:

public DomainTestWithinBorders(int xIn, float yIn, boolean out) {
    this.x = xIn;
    this.y = yIn;
    this.expectedOut = out;
}

(note that correct type of yIn is float, not int):


If you fix this you'll still get the following exception:

java.lang.IllegalArgumentException: argument type mismatch

To fix it, change:

Object[][] values = { { 0, 10.0, false }, { 1, 16.0, false },
        { 17, 17.0, true } };

to:

Object[][] values = { { 0, 10.0f, false }, { 1, 16.0f, false },
        { 17, 17.0f, true } };

OTHER TIPS

@Parameterized is too heavy tool for such a simple job. Try something simpler. For example you can test with parameters using zohhak:

import static junit.framework.Assert.assertEquals;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;

@RunWith(ZohhakRunner.class)
public class DomainTestWithinBorders {

    @TestWith({
        "0,  10.0, false",
        "1,  16.0, false",
        "17, 17.0, true"
    })
    public void testEqual(int x, float y, boolean expectedOut) {
        boolean actualOut = (x == y);
        assertEquals(expectedOut, actualOut);
    }   
}

I changed your constructor as well as some modifications in your data() method

@RunWith(Parameterized.class)
public class DomainTestWithinBorders extends TestCase {

    int x;

    float y;

    boolean expectedOut;

    public DomainTestWithinBorders(int xIn, float yIn, boolean out) {
        this.x = xIn;
        this.y = yIn;
        expectedOut = out;
    }

    @Test
    public void testEqual() {
        boolean actualOut = (x == y);
        assertEquals(expectedOut, actualOut);
    }

    @Parameters
    public static Collection< Object[] > data() {
        Object[][] values = { { 0, 10.0f, false } };
        return Arrays.asList(values);

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top