在我的项目中,我必须在所有测试之前进行一些存储库设置。这是使用一些棘手的静态规则完成的。但是,在所有测试之后,我不知道如何进行清理。我不想保留一些神奇的静态数字来指代所有测试方法的数量,我应该一直保持这些数字。
最受赞赏的方法是添加一些监听器,该监听器将在所有测试之后调用。 JUnit4中是否已经有任何接口(interface)?
编辑:这与@BeforeClass 和@AfterClass 无关,因为我必须知道是否最后一次调用了带有@AfterClass 注释的方法。
最佳答案
我正在使用 JUnit 4.9。这会有帮助吗?:
import junit.framework.TestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({First.class,Second.class,Third.class})
public class RunTestSuite extends TestCase {
@BeforeClass
public static void doYourOneTimeSetup() {
...
}
@AfterClass
public static void doYourOneTimeTeardown() {
...
}
}
编辑:我非常肯定(除非我误解了您的问题)我的解决方案就是您正在寻找的。即所有测试都运行后的一种拆卸方法。不需要监听器,JUnit 有这个功能。谢谢。
关于java - 所有junit测试后的清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9903341/