junit4 icon indicating copy to clipboard operation
junit4 copied to clipboard

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

Open pradzins opened this issue 13 years ago • 8 comments

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

pradzins avatar Aug 24 '11 10:08 pradzins

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.

dsaff avatar Sep 14 '11 14:09 dsaff

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

pradzins avatar Sep 15 '11 09:09 pradzins

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?

pradzins avatar Sep 15 '11 09:09 pradzins

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

dsaff avatar Sep 16 '11 14:09 dsaff

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

1hanymhajna avatar Dec 21 '15 16:12 1hanymhajna

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

kcooney avatar Dec 21 '15 17:12 kcooney

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 avatar Dec 22 '15 07:12 1hanymhajna

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

kcooney avatar Dec 23 '15 00:12 kcooney