Junit 5, контекст приложения Spring не закрывается на @DirtiesContext - PullRequest
0 голосов
/ 06 декабря 2018

Мой метод приложения не закрывается после метода тестирования.
Я использую Junit 5.3.1, Spring 5.1.0.RELEASE для тестов Selenium WebDriver.

Это мой компонент:

@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" закрыть браузери начать новый.Однако этого не происходит.Буду признателен за помощь

1 Ответ

0 голосов
/ 08 декабря 2018

Ну, я нашел обходной путь.Я реализовал весенние тестовые слушатели, и в слушателе я пометил контекст как грязный, вместо того, чтобы полагаться на аннотацию @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);

Он отмечаетгрязный контекст, и в следующем сеансе будет создан новый контекст.

...