简体   繁体   English

Junit 5,Spring Application Context在@DirtiesContext上未关闭

[英]Junit 5, Spring Application Context does not close on @DirtiesContext

My application context is not closed after test method. 测试方法后,我的应用程序上下文未关闭。
I use Junit 5.3.1, spring 5.1.0.RELEASE for Selenium WebDriver tests. 我在春季5.1.0.RELEASE中使用Junit 5.3.1进行Selenium WebDriver测试。

This is my bean: 这是我的豆子:

@Configuration
public class WebDriverConfig {

// ... Some Code ...

@Bean(destroyMethod = "quit")
    @Primary
    public DelegatingWebDriver cleanWebDriver(WebDriver driver) throws Exception {
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
        return new DelegatingWebDriver(driver);
    }

// ... Some more code ...
}

This is my class: 这是我的课:

 @ExtendWith({SpringExtension.class})
@ContextConfiguration(classes = { WebDriverConfig.class, LoggerConfig.class, EmailConfig.class})
@TestExecutionListeners(listeners= {ScreenshotTaker.class, DependencyInjectionTestExecutionListener.class, TestListener.class})
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class BasicScenariosIT {

    @Inject
    private DelegatingWebDriver driver;

    @SuppressWarnings("unused")
    @Inject
    private URI baseUrl;

    @Inject
    private Logger logger;

    private DelegatingExtentTest testCase;

// ... Some tests ...
}

I expect the line: 我期望这行:

@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)

to close the application context and fire up the line: 关闭应用程序上下文并启动该行:

@Bean(destroyMethod = "quit") @Bean(destroyMethod =“退出”)

In my case, call method "quit" close the browser and start a new one. 就我而言,调用方法“退出”关闭浏览器并启动一个新的浏览器。 However it doesn't seem to happen. 但是,这似乎没有发生。 Would appreciate the help 希望能有所帮助

Well, I found a workaround. 好吧,我找到了解决方法。 I implemented spring test listeners, and in the listener I marked the context as dirty, instead of relying on the @DirtiesContext annotation. 我实现了弹簧测试侦听器,并在侦听器中将上下文标记为脏,而不是依赖@DirtiesContext批注。 The listener looks like this: 侦听器如下所示:

@ExtendWith({SpringExtension.class})
@ContextConfiguration(classes = { WebDriverConfig.class, LoggerConfig.class})
@TestExecutionListeners(listeners= {DependencyInjectionTestExecutionListener.class})
public class RunnerExtension extends AbstractTestExecutionListener {


    @Autowired
    protected Logger logger;

    @Autowired
    protected DelegatingWebDriver driver;

    @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
        testContext.getApplicationContext()
                .getAutowireCapableBeanFactory()
                .autowireBean(this);        
    }

    @Override
    public void beforeTestMethod(TestContext testContext) throws Exception {
        // .. some code here ..
    }

    @Override
    public void beforeTestExecution(TestContext testContext) throws Exception {
        // .. some code here .. 
    }

    @Override
    public void afterTestExecution(TestContext testContext) throws Exception {
        // .. some code here ..
    }

    @Override
    public void afterTestMethod(TestContext testContext) throws IOException {

        // .. some code here ..

        testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE);

    }

    @Override
    public void afterTestClass(TestContext testContext) throws IOException {
        // .. some code here ..

    }
}

The important code line is: 重要的代码行是:

testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE); testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE);

It marks the context as dirty and a new context will be created in the next session. 它将上下文标记为脏,并在下一个会话中创建一个新的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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