Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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.

like image 408
Vlad Schnakovszki Avatar asked Mar 04 '13 23:03

Vlad Schnakovszki


1 Answers

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
like image 85
Ray Tayek Avatar answered Sep 28 '22 06:09

Ray Tayek