JUnit崩溃说方法应该是静态的,然后崩溃说它不应该?

时间:2014-01-28 17:56:56

标签: java unit-testing junit

我试图在JUnit中进行一个简单的示例测试,测试两件事,然后进行拆解。

import org.junit.*;

public class TestFoobar {
    @Test
    public void testOneThing() {
        // Code that tests one thing
    }

    @Test
    public void testAnotherThing() {
        // Code that tests another thing
    }

    @AfterClass
    @Test
    public void tearDownClass() throws Exception {
        // Code executed after the last test method 
    }
}

当我尝试运行它时,崩溃说tearDownClass必须是静态的:

$ javac TestFoobar.java -cp junit-4.8.2.jar
$ java -cp junit-4.8.2.jar:. org.junit.runner.JUnitCore TestFoobar
JUnit version 4.8.2
.E
Time: 0.01
There was 1 failure:
1) initializationError(TestFoobar)
java.lang.Exception: Method tearDownClass() should be static
    at org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:83)
    at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:66)
    at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:122)
    at org.junit.runners.ParentRunner.collectInitializationErrors(ParentRunner.java:104)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:119)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:269)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:66)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:58)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runner.Computer.getRunner(Computer.java:38)
    at org.junit.runner.Computer$1.runnerForClass(Computer.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:93)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
    at org.junit.runners.Suite.<init>(Suite.java:79)
    at org.junit.runner.Computer.getSuite(Computer.java:26)
    at org.junit.runner.Request.classes(Request.java:69)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:98)
    at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:45)

FAILURES!!!
Tests run: 1,  Failures: 1

所以我将tearDownClass更改为静态,但现在它说它不应该是静态的:

$ javac TestFoobar.java -cp junit-4.8.2.jar
$ java -cp junit-4.8.2.jar:. org.junit.runner.JUnitCore TestFoobar
JUnit version 4.8.2
.E
Time: 0.01
There was 1 failure:
1) initializationError(TestFoobar)
java.lang.Exception: Method tearDownClass() should not be static
    at org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:83)
    at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:66)
    at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:122)
    at org.junit.runners.BlockJUnit4ClassRunner.validateTestMethods(BlockJUnit4ClassRunner.java:200)
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:174)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:122)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:269)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:66)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:58)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runner.Computer.getRunner(Computer.java:38)
    at org.junit.runner.Computer$1.runnerForClass(Computer.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:93)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
    at org.junit.runners.Suite.<init>(Suite.java:79)
    at org.junit.runner.Computer.getSuite(Computer.java:26)
    at org.junit.runner.Request.classes(Request.java:69)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:98)
    at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:45)

FAILURES!!!
Tests run: 1,  Failures: 1

为什么?

1 个答案:

答案 0 :(得分:16)

你有无关的注释。它应该是:

@AfterClass
public static void tearDownClass() throws Exception {
    // Code executed after the last test method 
}

为了执行拆解,该方法本身应该是静态的,并且本身应该是@Test