Junit4: Categories + Parameterized 在 4.9 final 中仍然不起作用(#74 重新打开?)

创建于 2011-08-24  ·  8评论  ·  资料来源: junit-team/junit4

恐怕类别 + 参数化二重奏仍然无法在新的 junit 4.9 final 中正常工作,但在 #74 更改后,反馈比无消息 NPE 好得多。 请注意,测试类在类级别上具有@Category注释。
堆栈跟踪:

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)

代码片段 - 为简洁起见省略了导入,如有必要,我可以为您提供完整的 eclipse/ant 项目。
有问题的测试:

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

    <strong i="12">@Test</strong>
    public void addAndSeeWhatHappens() throws Exception {
        assertEquals("sum", expectedSum, one + two);
    }

    <strong i="13">@Parameters</strong>
    public static Collection<Integer[]> regExValues() {
        return Arrays.asList(SCENARIOS);
    }

}

套房:

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

}

当我在 AdditionTest 中注释掉@Category时,该套件可以正常运行 MultiplicationTest(它包含“普通”测试方法,也具有类级别的 @Category)。

bug parameterized regression

所有8条评论

帕维尔,

感谢您的错误报告。 我认为附加的提交会修复它。 在接下来的 24 小时内,您有时间快速浏览一下吗? 谢谢。

是的,会尝试。 几个小时后就会回来。 谢谢你。

它现在完美运行 - 非常感谢。
我从你的分类参数修复创建了一个分支,构建了 junit4.10-SNAPSHOT,现在参数化和类别愉快地生活在一起。 更重要的是,我验证了添加到套件中的理论也是如此(实际上之前也是如此),例如:

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

    <strong i="7">@DataPoints</strong>
    public static int[] someNumbers = { 1, 4, -5, 10, -100, -3, 0, 45, -997 };

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

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

修复程序是否有可能使其成为 4.9.1?

实际上,我正在跳过 4.9.1,并将在下周左右发布 4.10,其中包含此修复程序。

它仍然不适合我,我正在使用 junit 4.12

@1hanymhajna你能给我们一个最小的测试用例吗?

当然:
@RunWith(Parameterized.class)
公共课 myClass {

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

@Category(myCategory.class)
@测试
公共无效我的测试(){
断言.assertTrue(真);
}

}

如果我在没有类别的情况下运行此测试,它对我来说工作正常,但是如果我将类别与参数化类一起放置,则会引发异常并显示此错误消息:单个方法不支持参数化类的类别注释

@1hanymhajna感谢您的快速回复。

我们可能会考虑解决这个问题,但在 JUnit5 中,我们将支持参数化测试,而无需特殊的运行器。 我建议提交一个错误以确保 JUnit5 支持类别 + 参数。

此页面是否有帮助?
0 / 5 - 0 等级