Junit4: Categories + Parameterized still does not work in 4.9 final (#74 reopen?)

Created on 24 Aug 2011  ·  8Comments  ·  Source: junit-team/junit4

I am afraid the categories + parameterized duo still does not work properly with fresh junit 4.9 final, yet after #74 changes the feedback is much better than messageless NPE. Note that the test class has @Category annotation on class level.
The stacktrace:

java.lang.Exception: Category annotations on Parameterized classes are not supported on individual methods.
    at org.junit.runners.model.InitializationError.<init>(InitializationError.java:30)
    at org.junit.experimental.categories.Categories.assertNoDescendantsHaveCategoryAnnotations(Categories.java:179)
    at org.junit.experimental.categories.Categories.assertNoCategorizedDescendentsOfUncategorizeableParents(Categories.java:171)
    at org.junit.experimental.categories.Categories.assertNoCategorizedDescendentsOfUncategorizeableParents(Categories.java:173)
    at org.junit.experimental.categories.Categories.<init>(Categories.java:156)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:35)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Code snippets - imports omitted for brevity, I can provide you with a complete eclipse/ant project if necessary.
The problematic test:

@RunWith(Parameterized.class)
@Category(SampleCategory.class)
public class AdditionTest {

    static final Integer[][] SCENARIOS = new Integer[][] { { 2, 2, 4 },
            { -2, 2, 0 }, { -4, -1, -5 }, };
    int one, two, expectedSum;

    public AdditionTest(int one, int two, int expectedSum) {
        super();
        this.one = one;
        this.two = two;
        this.expectedSum = expectedSum;
    }

    @Test
    public void addAndSeeWhatHappens() throws Exception {
        assertEquals("sum", expectedSum, one + two);
    }

    @Parameters
    public static Collection<Integer[]> regExValues() {
        return Arrays.asList(SCENARIOS);
    }

}

The suite:

@RunWith(Categories.class)
@IncludeCategory(SampleCategory.class)
@SuiteClasses({ AdditionTest.class, MultiplicationTest.class })
public class SampleSuite {

}

When I comment out the @Category in AdditionTest, the suite runs MultiplicationTest fine (it contains "normal" test methods, also has class-level @Category).

bug parameterized regression

All 8 comments

Pawel,

Thanks for the bug report. I think the attached commit will fix it. Do you have time to give it a quick look in the next 24 hours? Thanks.

Yes, will try. Will be back in a few hours. Thank you.

It works perfectly now - thanks a lot.
I created a branch from your categorized_parameters_fix, built junit4.10-SNAPSHOT and now Parameterized and Category live together happily. What's more, I verified the same is true (and actually was before) for Theories added to a suite, such as:

@RunWith(Theories.class)
@Category(SampleCategory.class)
public class NumberTheoryOrSomething {

    @DataPoints
    public static int[] someNumbers = { 1, 4, -5, 10, -100, -3, 0, 45, -997 };

    @Theory
    public void positiveTheory(int one, int two) {
        assumeTrue(one >= 0 && two >= 0);
        assertTrue("The result should not be negative", one * two >= 0);
    }

    @Theory
    public void surprisinglyPositiveTheory(int one, int two) {
        assumeTrue(one < 0 && two < 0);
        assertTrue("The result should be positive", one * two >= 0);
    }
}

Is there any chance the fix will make it to 4.9.1?

Actually, I'm skipping 4.9.1, and will be releasing 4.10 in the next week or so, with this fix included.

it's still not working for me , i'm using junit 4.12

@1hanymhajna can you give us a minimal test case?

sure:
@RunWith(Parameterized.class)
public class myClass {

 @Parameterized.Parameters(name = "{index}: params for test:({0})")
public static ArrayList<String[]> loadProcessorList() throws IOException {
    return MyParams.loadParams();
}

@Category(myCategory.class)
@Test
public void myTest(){
Assert.assertTrue(true);
}

}

if i run this test without category it's working fine for me , but if i put category with parameterized class it's throw an exception with this error message : Category annotations on Parameterized classes are not supported on individual methods

@1hanymhajna thanks for the quick response.

We can possibly look into fixing this, but in JUnit5 we will support parameterized tests without a special runner. I suggest filing a bug to ensure JUnit5 supports categories + parameters.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Reissner picture Reissner  ·  3Comments

apreg picture apreg  ·  4Comments

akarnokd picture akarnokd  ·  27Comments

forvoid picture forvoid  ·  8Comments

lvc picture lvc  ·  6Comments