我现在正在编写测试,并且意识到它们一开始都非常相似:
@Test
public void testThisMemoryOperation() {
Assert.assertEquals("Sanity check failed, shouldn't be anything in memory yet.",
0, memoryManager.getObjectsInMemory());
/* ... */
}
@Test
public void testThatMemoryOperation() {
Assert.assertEquals("Sanity check failed, shouldn't be anything in memory yet.",
0, memoryManager.getObjectsInMemory());
/* ... */
}
@Test
public void testTheOtherMemoryOperation() {
Assert.assertEquals("Sanity check failed, shouldn't be anything in memory yet.",
0, memoryManager.getObjectsInMemory());
/* ... */
}
这似乎是不必要的重复。我可以在每个测试开始时用一个简单的方法调用来替换这段代码来运行健全性检查断言,但是有没有像@Rule
我可以用来在运行某些方法之前简单地运行这个测试的本机 JUnit 注释?