samedi 31 octobre 2020

Is there a way to get event in spring (+junit) fired after all tests classes?

I use spring 5 + junit 5. And I have two classes - BarIT and FooIT.

@ExtendWith({SpringExtension.class})
@ContextConfiguration(classes = ModuleContextConfig.class)
public class FooIT {

    @Test
    public void foo() {
        System.out.println("Foo");
    }
}

@ExtendWith({SpringExtension.class})
@ContextConfiguration(classes = ModuleContextConfig.class)
public class BarIT {

    @Test
    public void bar() {
        System.out.println("Bar");
    }
}

This is my suite:

@RunWith(JUnitPlatform.class)
@ExtendWith({SpringExtension.class})
@SelectClasses({
    FooIT.class,
    BarIT.class
})
@IncludeClassNamePatterns(".*IT$")
public class SuiteIT {

}

I want to get event when tests in two classes have been executed, I mean event after FooIT.foo() and BarIT.bar(), however, I can't do that. I hoped to get ContextClosedEvent but it is not fired:

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ApplicationListenerBean implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("Event:" + event.toString());
    }
}

And this is the output:

Event:org.springframework.context.event.ContextRefreshedEvent..
Event:org.springframework.test.context.event.PrepareTestInstanceEvent..
Event:org.springframework.test.context.event.BeforeTestMethodEvent..
Event:org.springframework.test.context.event.BeforeTestExecutionEvent..
Foo
Event:org.springframework.test.context.event.AfterTestExecutionEvent...
Event:org.springframework.test.context.event.AfterTestMethodEvent...
Event:org.springframework.test.context.event.AfterTestClassEvent...
Event:org.springframework.test.context.event.BeforeTestClassEvent...
Event:org.springframework.test.context.event.PrepareTestInstanceEvent...
Event:org.springframework.test.context.event.BeforeTestMethodEvent...
Event:org.springframework.test.context.event.BeforeTestExecutionEvent...
Bar
Event:org.springframework.test.context.event.AfterTestExecutionEvent...
Event:org.springframework.test.context.event.AfterTestMethodEvent...
Event:org.springframework.test.context.event.AfterTestClassEvent..

Could anyone say how to do it, if it is possible.

Aucun commentaire:

Enregistrer un commentaire