Pertanyaan JUnit crash mengatakan metode harus statis, kemudian crash mengatakan seharusnya tidak?


Saya mencoba untuk membuat contoh sederhana di JUnit yang menguji dua hal, lalu melakukan teardown.

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

Ketika saya mencoba untuk menjalankan ini, itu crash mengatakan tearDownClass harus statis:

$ 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

Jadi saya berubah tearDownClass ke statis, tetapi sekarang dikatakan itu tidak boleh statis:

$ 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

Mengapa?


5
2018-01-28 17:56


asal


Jawaban:


Anda memiliki anotasi yang asing. harus:

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

Metode itu sendiri harus statis untuk melakukan teardown, dan itu sendiri seharusnya tidak menjadi @Test.


16
2018-01-28 17:58