简体   繁体   中英

@AfterEach annotation ignored in Spring test

I'm writing some integration tests utilizing Spring. I cannot get @AfterEach to be respected. I assume it's a version issue or library conflict? I've done this before but unsure why it's not being respected now.

import org.springframework.test.context.junit4.SpringRunner;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.runner.RunWith;


@SpringBootTest(classes = ConsumerService.class)
@ContextConfiguration(classes = {KafkaIntegrationConfig.class})
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class AuthenticationTransformationTest {

   @AfterEach
   public void afterEach() {
     // this is ignored
   }
}

I do not know how I hit your questions for the third time today (not on purpose.)... I would start by figuring out my classpath if I were you. @RunWith comes from junit4 and @AfterEach from junit5 , thus the obvious thing that does (not) happen.

You really want to stick with junit-5 most probably. And if you are questioning where do you get both junit4 and junit5 , most probably from spring-boot-starter-test and an explicit dependency that you have defined. You can always issue gradle dependencies look at the output and figure out what dependency is brought by what jar .

Here is an example how exclusion would happen:

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    exclude group: 'org.mockito', module: 'mockito-core'
    exclude group: 'org.mockito', module: 'mockito-junit-jupiter'
}     

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