Is there way to define a different teardown for each @Test in jUnit?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
There is no easy way to do this in JUnit. With TestNG, you can put your methods in groups, so you can define specific @BeforeMethod/@AfterMethod that only get run around certain groups.
Use the
@After
annotation to indicate the method(s) to be run after every@Test
.The full suite of annotations like this are:
@BeforeClass
- before all@Tests
are run@Before
- before each@Test
is run@After
- after each@Test
is run@AfterClass
- after all@Tests
are runI 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:
The point of grouping test methods together in the same class is so they can share things, that includes having the same setup and teardown. So, yes, you can define separate teardowns for each test, but you do so by putting the @Test methods in different classes.
Once you start having separate teardown methods the rationale for why you would want to group the tests together in the same class is not apparent. So you could manage this situation by being flexible about how you group your tests in classes.