I am new to Junit. I was learning that we have to use @BeforeEach or @Before to setup a new instance. I also understood that when we use @AfterEach or @Afterclass is a teardown method which releases them after all tests. However, I am curious if anything is going to be affected when we do not use @AfterEach or @AfterClass because when we use @BeforeEach we would get new instance anyways. Additionally, what does release means here, I want to know what is happening when we use @AfterEach.
Asked
Active
Viewed 447 times
0
-
It does what you program it to do. Indeed, as you suggested, very often you do not need any `@After*` method as the GC will take care of old objects and you'll simply get new ones for a new test. That said, sometimes you have to do some cleanup - delete files, rollback DB transactions, close DB connections etc. In those cases the methods can be useful. – Petr Janeček Dec 14 '21 at 13:08
1 Answers
0
It does what you program it to do. Indeed, as you suggested, very often you do not need any @After*
method as the GC will take care of old objects and you'll simply get new ones for a new test.
That said, sometimes you have to do some cleanup - delete files, rollback DB transactions, close DB connections etc. In those cases the methods can be useful. Once your code becomes more complex, your tests and test setup will too become more complex.

Petr Janeček
- 37,768
- 12
- 121
- 145