简体   繁体   English

Junit之前/之后以及设置/拆卸

[英]Junit Before/After and setup/teardown

Are the setUp and teardown (precondition and postcondition settings) identified by the name setUp() and teardown() or by @Before and @After ? 是通过名称setUp()teardown()还是@Before@After标识setUpteardown (前提条件和后置条件设置)的?

In other words is it possible in JUnit to have a @Before teardown() and a @After setUp() ? 换句话说,在JUnit中是否可以具有@Before teardown()@After setUp()

JUnit 3: the methods are identified by name setUp and tearDown JUnit 3:方法由名称setUptearDown标识

JUnit 4: the methods are identified by annotation @Before and @After JUnit 4:方法通过注释@Before@After标识

JUnit 5: the methods are identified by annotation @BeforeEach and @AfterEach JUnit 5:方法通过注释@BeforeEach@AfterEach

edit: To answer your question, yes it is possible to write 编辑:要回答您的问题,是的,可以写

@Before
public void tearDown() {...}

but this would not increase the readability of your code. 但这不会增加代码的可读性。

It depends on the version of JUnit that you use. 这取决于您使用的JUnit版本。 In case you have annotations, it's most probably JUnit 4. In such case it doesn't matter how you name your methods, what is more important is that which annotations you use. 如果您有注释,则很可能是JUnit4。在这种情况下,方法的命名方式无关紧要,更重要的是,您使用了哪些注释。 Usage of annotations helps in defining clear logic without "magic" with method names. 注释的使用有助于定义清晰的逻辑,而无需对方法名称进行“魔术”处理。 If you use Junit 4, you can call methods whatever you want, you just need put corresponding annotations above them. 如果使用Junit 4,则可以调用任何方法,只需在它们上方放置相应的注释即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM