我需要在test之前在junit中运行一次Initialize代码 - java

时间:2018-04-20 07:45:17

标签: java junit annotations

我需要在test之前在junit中运行一次Initialize代码 - java。

如果我们选择 @Before

它将在每个测试空白之前运行。

Object me;

public void InstalizeTestClass()}

me = configReader.read();

}

@Test
public void SumTest(){

}

当我使用@BeforeClass时发现了exeption java.lang.Exception:方法InstalizemockTest()应该是静态的

at org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:93)
at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:74)
at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:155)
at org.junit.runners.ParentRunner.collectInitializationErrors(ParentRunner.java:125)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:124)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
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:33)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

1 个答案:

答案 0 :(得分:3)

您可以使用@BeforeClass注释:

@BeforeClass
public static void myLogic() {
  // Code that needs to run once
}