测试后清除Spring应用程序上下文 [英] Clear Spring application context after test

查看:124
本文介绍了测试后清除Spring应用程序上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每次测试执行后,如何使用Junit5和Spring Boot清除应用程序上下文?我希望在测试中创建的所有bean在执行后都被销毁,因为我要在多个测试中创建相同的bean.我不想为所有测试都使用一个配置类,但是每个测试都需要一个配置类,如下所示.

How can I clear the application context after each test execution, with Junit5 and Spring Boot? I want all beans created in the test to be destroyed after its execution, since I am creating the same beans in multiple tests. I don't want to have one configuration class for all tests, but configuration class per test, as shown bellow.

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = MyTest.ContextConfiguration.class)
public class MyTest{
   ...
   public static class ContextConfiguration {
     // beans defined here... 

   }
}

放入 @DirtiesContext(classMode = BEFORE_CLASS)与Junit5不兼容.

Putting @DirtiesContext(classMode = BEFORE_CLASS) doesn't work with Junit5.

推荐答案

您两次声明 @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)不起作用;但是,以下显示了它的工作情况.

You have claimed twice that @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) does not work; however, the following shows that it works as documented.

import javax.annotation.PreDestroy;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@ContextConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
class MyTest {

    @Test
    void test1(TestInfo testInfo) {
        System.err.println(testInfo.getDisplayName());
    }

    @Test
    void test2(TestInfo testInfo) {
        System.err.println(testInfo.getDisplayName());
    }

    @Configuration
    static class Config {

        @Bean
        MyComponent myComponent() {
            return new MyComponent();
        }
    }

}

class MyComponent {

    @PreDestroy
    void destroy() {
        System.err.println("Destroying " + this);
    }
}

执行上述测试类将导致输出到 STDERR ,类似于以下内容.

Executing the above test class results in output to STDERR similar to the following.

test1(TestInfo)
Destroying MyComponent@dc9876b
test2(TestInfo)
Destroying MyComponent@30b6ffe0

因此, @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)确实是您在每次测试执行后清除应用程序上下文"的方式.

Thus, @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) is indeed how you "clear the application context after each test execution".

放入 @DirtiesContext(classMode = BEFORE_CLASS)与Junit5不兼容.

Putting @DirtiesContext(classMode = BEFORE_CLASS) doesn't work with Junit5.

根据我的测试, classMode = BEFORE_CLASS 可与TestNG,JUnit 4和JUnit Jupiter(又称JUnit 5)一起使用.

Based on my testing, classMode = BEFORE_CLASS works with TestNG, JUnit 4, and JUnit Jupiter (a.k.a., JUnit 5).

因此,如果这在您的测试类中不起作用,那将是一个错误,您应该

So, if that does not work in your test class, that would be a bug which you should report to the Spring Team.

您是否有任何示例可以证明它不起作用?

Do you have any example where you can demonstrate that not working?

仅供参考:如果已经在当前执行的测试套件中创建了上下文,则使用 classMode = BEFORE_CLASS 才有意义.否则,您将指示Spring关闭并从不存在的缓存中删除 ApplicationContext ...就在Spring实际创建它之前.

FYI: using classMode = BEFORE_CLASS would only ever make sense if the context had already been created within the currently executing test suite. Otherwise, you are instructing Spring to close and remove an ApplicationContext from the cache that does not exist... just before Spring actually creates it.

此致

Sam(Spring TestContext Framework的作者)

Sam (author of the Spring TestContext Framework)

这篇关于测试后清除Spring应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆