Vlad Schnakovszki
Vlad Schnakovszki

Reputation: 8601

Java JUnit Parameterized error

I am creating a Java application based on JRE 6. I use JUnit 4 to generate parameterized tests. I am receiving this error:

The annotation @Parameterized.Parameters must define the attribute value

on the line containing the annotation:

@Parameterized.Parameters

Below is the code I believe to be relevant to this issue:

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import calc.CalculatorException;
import calc.ScientificCalculator;

@RunWith(Parameterized.class)
public class ScientificCalculatorTest extends BasicCalculatorTest{

    /** Provides an interface to the scientific features of the calculator under test */
    private ScientificCalculator sciCalc;
    private double a, b;


    @Before
    @Override
    public void setUp() throws Exception {
        sciCalc = new ScientificCalculator();
        //Make sure that the basic functionality of the extended calculator
        //hasn't been broken.
        theCalc = sciCalc;
    }

    /**
     * Constructor. Is executed on each test and sets the test values to each pair in the data sets.
     * @param nr1 the first number in the tested pair.
     * @param nr2 the second number in the tested pair.
     */
    public ScientificCalculatorTest(double nr1, double nr2){
        a = nr1;
        b = nr2;
    }


    @Parameterized.Parameters
    public static Collection<Object[]> testGenerator() {
        return Arrays.asList(new Object[][] {
                //General integer values | -/+ combinations
                {  -100,  -100},
                {  -100,   100},
                {   100,  -100},
                {   100,   100}
        });
    }

I managed to find some far related questions, such as this. Sadly, in my situation they're of no help.

What I have tried and didn't work:

I need to mention that I have used a very similar implementation (most notably the annotations and testGenerator()) in another project without any issues. The implementation follows the tutorials available online, such as this, this and this.

Any help on solving this error is greatly appreciated.

Upvotes: 12

Views: 7785

Answers (4)

velocity
velocity

Reputation: 2106

I had the same problem, i was extending my test from a test super class which has an init() method annotated with @org.junit.Before .
I implemented a test in the child class and run it, everything was fine so far.
Then i wanted to use a parameterized annotation to repeat the test for different values, so i used @ParameterizedTest with a @ValueSource and i run the test but it did not work because the initialization method in the supper class was not executed.
I overwritten the init() method in the child class and called super.init() but it did not work.
A solution that worked is to call super.init() at the start of the test in the child class.
I think this is a compatibility problem because everytime i mix JUnit4 and JUnit5 annotations in the same test class something wrong happens.

Upvotes: -2

Johne Altamera
Johne Altamera

Reputation: 224

You miss the below import I think.

import org.junit.runners.Parameterized.Parameters;

Upvotes: 1

Ayub Malik
Ayub Malik

Reputation: 2578

It must be because you extend BaseTestCase. I copied your code without extending from a base class the tests run correctly.

Try calling super.setUp() in your setup

E.g.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    sciCalc = new ScientificCalculator();
    //Make sure that the basic functionality of the extended calculator
    //hasn't been broken.
    theCalc = sciCalc;
}

Upvotes: 0

Ray Tayek
Ray Tayek

Reputation: 10013

try this:

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
public class So15213068 {
    public static class BaseTestCase {
        @Test public void test() {
            System.out.println("base class test");
        }
    }
    @RunWith(Parameterized.class) public static class TestCase extends BaseTestCase {
        public TestCase(Double nr1,Double nr2) {
            //super(nr1,nr2);
            this.nr1=nr1;
            this.nr2=nr2;
        }
        @Test public void test2() {
            System.out.println("subclass test "+nr1+" "+nr2);
        }
        @Parameters public static Collection<Object[]> testGenerator() {
            return Arrays.asList(new Object[][]{{-100.,-100.},{-100.,100.},{100.,-100.},{100.,100.}});
        }
        double nr1,nr2;
    }
}

output:

subclass test -100.0 -100.0
base class test
subclass test -100.0 100.0
base class test
subclass test 100.0 -100.0
base class test
subclass test 100.0 100.0
base class test

Upvotes: 1

Related Questions