jUnit中每个@Test都有不同的拆解 [英] Different teardown for each @Test in jUnit
本文介绍了jUnit中每个@Test都有不同的拆解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法为jUnit中的每个@Test定义一个不同的拆解?
Is there way to define a different teardown for each @Test in jUnit?
推荐答案
使用 @After
注释,指示在每个 @Test
之后运行的方法。
Use the @After
annotation to indicate the method(s) to be run after every @Test
.
这样的全套注释是:
-
@BeforeClass
- 在所有@Tests
运行之前 -
@Before
- 每个@Test
正在运行 -
@After
- 每个@Test
正在运行 -
@AfterClass
- 毕竟@Tests
正在运行
@BeforeClass
- before all@Tests
are run@Before
- before each@Test
is run@After
- after each@Test
is run@AfterClass
- after all@Tests
are run
我刚才意识到我可能没有理解这个问题。如果您询问如何将特定的拆解方法与特定的@Test方法相关联,则无需注释:只需在最终的测试方法结束时调用它:
I just realised I may not have understood the question. If you are asking how to associate a particular teardown method to a particular @Test method, there is no need for annotations: Simply call it at the end of your test method in a finally:
@Test
public void someTest() {
try {
// test something
} finally {
someParticularTearDown();
}
}
这篇关于jUnit中每个@Test都有不同的拆解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文