具有本地内部类的JUnit 4测试方法在测试用例中不一致

时间:2016-07-10 17:06:12

标签: java unit-testing junit junit4 inner-classes

好的,我尽我所能搜索并找不到任何东西。我是第一次问问,所以要温柔。 :P

在一个测试用例中,我有一个测试方法,用于在触发时测试给定的事件侦听器方法。

public class TestCaseDoingSupport {

    public TestCaseDoingSupport() {
        super();
    }
    @Test
    public void testFireEventDoingSomething() {

       class LocalDoingListener extends DoingAdapter {

                    public LocalDoingListener() {
                        super();
                    }
                    public void onStartedDoingSomething(DoingEvent event) {
                        Assert.assertEquals(DoingClass.STARTED_DOING_SOMETHING, event.getDoingStatus());
                    }
                }

                DoingListener listener = new LocalDoingListener();
                DoingSupport support = new DoingSupport(new DoingClass());
                support.addDoingListener(listener);
                support.fireStartedDoingSomething();
            }
}

我遇到的问题是:这个完全模式用于另外两个测试用例(在同一个项目中),用于其他两个事件监听器,它运行得非常好,但在测试用例中我我得到的问题(来自测试报告)java.lang.Exception: Test class should have exactly one public constructor

我真的不明白为什么这个模式在一个测试用例中起作用而在另一个测试用例中不起作用 - 我甚至尝试将构造函数取出,但它没有任何区别。有没有人遇到过这个问题,如果有的话,你是怎么解决的?

非常感谢。

修改 我尝试将方法的构造函数 - 本地类公开(反映在发布的代码片段中),但测试报告中的投诉是相同的。在命令行中, gradle 报告:

com.example.testTestCaseDoingSupport$1LocalDoingListener > initializationError FAILED
    java.lang.Exception

下面发布了完整的堆栈跟踪。

java.lang.Exception: Test class should have exactly one public zero-argument constructor
    at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:147)
    at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:124)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:84)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:56)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:1)

好的,我找到了。在我的 gradle 脚本中,我有这个

tasks.withType(Test) {
    scanForTestClasses = false
    exclude "**/*\$5*.class"
}

避免将内部类视为测试类。将此编辑为exclude "**/*\$*.class"解决了所有内部类。

我无法相信我之前没有看到这个......