简体   繁体   中英

If an exception is thrown in the @BeforeEach method, will @AfterEach still run

In my tests, I have data setup (which is a series of different entities) that happens in the @BeforeEach method. I clean up that data in the @AfterEach method. Now if an exception were to be thrown during the creation of any of those entities, will the @AfterEach method run? Currently I have a workaround with a try/catch block in my @BeforeEach method, but it would reduce some code duplication if the @AfterEach method takes care of it for me.

So after further testing, the @AfterEach method runs after the @BeforeEach method in the event an exception occurs in the @BeforeEach method. The @BeforeEach method is doing data setup in the DB. It run a series of API calls to create all the data which is persisted in the DB. Then the test runs, followed by the @AfterEach method. If an exception occurs while creating an entity, the @BeforeEach method terminates, the @AfterEach method is invoked which cleans up any created entities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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