QuasarLex
QuasarLex

Reputation: 85

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?

In other words is it possible in JUnit to have a @Before teardown() and a @After setUp()?

Upvotes: 2

Views: 3491

Answers (2)

Roland Weisleder
Roland Weisleder

Reputation: 10511

JUnit 3: the methods are identified by name setUp and tearDown

JUnit 4: the methods are identified by annotation @Before and @After

JUnit 5: the methods are identified by annotation @BeforeEach and @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.

Upvotes: 7

dvelopp
dvelopp

Reputation: 4295

It depends on the version of JUnit that you use. 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. 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.

Upvotes: 2

Related Questions