【问题标题】:@AfterEach annotation ignored in Spring test@AfterEach 注释在 Spring 测试中被忽略
【发布时间】:2021-03-21 23:28:24
【问题描述】:

我正在使用 Spring 编写一些集成测试。我无法让@AfterEach 受到尊重。我认为这是版本问题或库冲突?我以前这样做过,但不确定为什么现在没有得到尊重。

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
   }
}

【问题讨论】:

    标签: java spring spring-boot testing


    【解决方案1】:

    我不知道我今天是如何第三次问到你的问题(不是故意的!)...如果我是你,我会先弄清楚我的类路径。 @RunWith 来自junit4@AfterEach 来自junit5,因此显而易见的事情确实(不会)发生。

    真的很可能想坚持使用junit-5。如果你想知道junit4junit5 从哪里得到both,很可能来自spring-boot-starter-test 和你定义的显式依赖。您可以随时发出gradle dependencies 查看输出并找出jar 带来的依赖关系。

    以下是排除如何发生的示例:

    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'
    }     
    

    【讨论】:

    • 就是这样。我有一些奇怪的冲突,并得到了解决。现在一切都很完美。谢谢尤金!
    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 2019-05-14
    • 2013-11-05
    • 2014-10-06
    • 1970-01-01
    • 2017-06-24
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多