java - Junit 5,Spring 应用程序上下文未在 @DirtiesContext 上关闭

标签 java spring selenium-webdriver junit junit5

测试方法后我的应用程序上下文未关闭。
我使用 Junit 5.3.1、spring 5.1.0.RELEASE 进行 Selenium WebDriver 测试。

这是我的 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 ...
}

这是我的课:

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

我期待这行:

@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)

关闭应用程序上下文并启动该行:

@Bean(destroyMethod = "quit")

就我而言,调用方法“quit”关闭浏览器并启动一个新浏览器。然而这似乎并没有发生。 非常感谢您的帮助

最佳答案

嗯,我找到了一个解决方法。 我实现了 spring 测试监听器,并在监听器中将上下文标记为脏,而不是依赖 @DirtiesContext 注释。 监听器看起来像这样:

@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 ..

    }
}

重要的代码行是:

testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE);

它将上下文标记为脏,并且将在下一个 session 中创建新的上下文。

关于java - Junit 5,Spring 应用程序上下文未在 @DirtiesContext 上关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53658764/

相关文章:

java - 使用 setvisible 方法使用表单元素

java - 如何将 url 参数绑定(bind)到 jax-rs 中的 Controller 参数对象

java - Maven 项目不会在 selenium 项目的 test-output 文件夹中生成 testng 结果

java - 如何使用mybatis在postgres中将int[]映射到integer[]

Java相当于python环境错误?

java - 如何查找同一 xml 元素 id 值的所有多次使用?

java - Spring 的 RedirectAttributes 中 getFlashAttributes() 的使用

java - 在不同的 jar 文件中使用 @RestControllerAdvice 不会捕获异常

java sendkeys 错误键应该是一个字符串

python - 从数据框中读取网络链接会引发 "stale element reference: element is not attached to the page document"错误