12

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:

  • removing the "extends BasicCalculatorTest" from the class declaration

  • adding test functions that use the @Test annotation

  • importing org.junit.runners.Parameterized and using @Parameters instead of @Parameterized.Parameters

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.

13
  • 2
    @Parameterized.Parameters(value=/*required here*/) the error says the attribute value is mandatory. Commented Mar 4, 2013 at 23:31
  • @PaulBellora, it was just a typo, thanks for pointing it out, I have corrected it but the problem still remains. Commented Mar 4, 2013 at 23:32
  • @BheshGurung, I know it says that but I have used it in another project without (value=/*required here*/) and it worked just fine. Also, none of the tutorials I have linked use this. Commented Mar 4, 2013 at 23:34
  • 1
    @VladSchnakovszki I ask because I was able to get the exact same code, sans super class, to work on my machine. Commented Mar 5, 2013 at 17:40
  • 1
    Can you post a snapshot of what your projects has in the "Referenced Libraries" section? Commented Jun 4, 2013 at 8:17

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

this runs fine for me using jdk7 and eclipse on win 7 x64
i forgot to extend the base test case. i made that edit and added the output.
Thank you for your answer. Sadly, I gave a too short initial response to your solution. If I create a new project, add the JUnit4 libraries and add your code, it works perfectly. The problem is that if I add the code to the project in which I have encountered the problem, it shows the same error. I am thinking at this point that it is probably a wrong configuration in the project's properties. Any ideas from here?
maybe something funny in your old project. try creating a new project and moving everything or remove and add everything in the build path on the old project or move project to a new machine.
Maybe your old project has an old version of JUnit in its classpath?
|
1

You miss the below import I think.

import org.junit.runners.Parameterized.Parameters;

Comments

0

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

Comments

-2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.