13

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.

Community
  • 1
  • 1
Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
  • 2
    `@Parameterized.Parameters(value=/*required here*/)` the error says the attribute `value` is mandatory. – Bhesh Gurung Mar 04 '13 at 23:31
  • @PaulBellora, it was just a typo, thanks for pointing it out, I have corrected it but the problem still remains. – Vlad Schnakovszki Mar 04 '13 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. – Vlad Schnakovszki Mar 04 '13 at 23:34
  • Have you checked the documentation of the version you are using? It might have changed since you used it last time. – Bhesh Gurung Mar 04 '13 at 23:36
  • @BheshGurung, I have tested the previous (working) project now, as another project in the same instance of Eclipse and it works perfectly, while this one does not. Since under the same conditions, one works but the other doesn't, I would say it is something in this implementation rather than a problem with the version of Java. – Vlad Schnakovszki Mar 04 '13 at 23:39
  • I shouldn't have anything to do with Java itself. It's just how the annotation itself is define. May be the one in your version is defined with that constraint. You should check the documentation (or the source). – Bhesh Gurung Mar 04 '13 at 23:43
  • Can you give us more information about the super class? –  Mar 05 '13 at 00:31
  • i have code that works by just using @Parameters – Ray Tayek Mar 05 '13 at 01:07
  • @RayTayek so do I in another project, but for some reason, in this project it does not work. – Vlad Schnakovszki Mar 05 '13 at 15:50
  • @KurtKaylor, it should not be relevant since the problem also occurs if I do not extend BasicCalculatorTest. – Vlad Schnakovszki Mar 05 '13 at 15:53
  • 1
    @VladSchnakovszki I ask because I was able to get the exact same code, sans super class, to work on my machine. –  Mar 05 '13 at 17:40
  • 1
    Can you post a snapshot of what your projects has in the "Referenced Libraries" section? – Volker Stolz Jun 04 '13 at 08:17
  • Works for me (Without BasicCalculatorTest extension). So this might be a problem in inheritance what line does this error points to, what version of junit do you use? – mavarazy Sep 09 '13 at 10:08

4 Answers4

2

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
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • this runs fine for me using jdk7 and eclipse on win 7 x64 – Ray Tayek Mar 05 '13 at 17:27
  • i forgot to extend the base test case. i made that edit and added the output. – Ray Tayek Mar 05 '13 at 17:31
  • 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? – Vlad Schnakovszki Mar 05 '13 at 18:55
  • 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. – Ray Tayek Mar 05 '13 at 19:08
  • Maybe your old project has an old version of JUnit in its classpath? – Mikkel Apr 10 '13 at 10:26
  • @VladSchnakovszki Have you tried removing all your jars from your projects and adding them again? For example Parameterized class and Parameter.class are provided by many libraries except Junit.jar. – Ravi A Apr 23 '13 at 12:37
1

You miss the below import I think.

import org.junit.runners.Parameterized.Parameters;
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Johne Altamera
  • 204
  • 1
  • 2
  • 7
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;
}
Ayub Malik
  • 2,488
  • 6
  • 27
  • 42
-1

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.

velocity
  • 1,630
  • 21
  • 24