'JUnit5 Spock 2, @ExtendWith does not work

i am trying to set up testing platform for our new project. We want to use Spock 2 with JUnit 5.

And now i want to use @ExtendWith annotation in Spock based test.

Here is an example:

@SpringBootTest(classes = ExtensionTest.class)
@ExtendWith(ClassExtension.class)
class ExtensionTest extends Specification {
    void "test"() {
        when:
        true

        then:
        noExceptionThrown()
    }
}

Given Extension (ClassExtension) is implementing interfaces: BeforeAllCallback and AfterAllCallback.

class ClassExtension implements BeforeAllCallback, AfterAllCallback {
    @Override
    void beforeAll(ExtensionContext extensionContext) throws Exception {
       ...
    }

    @Override
    void afterAll(ExtensionContext extensionContext) throws Exception {
       ...
    }
}

Unfortunately it doesn't work. Extension is not being run.

What am I doing wrong?



Solution 1:[1]

Spock 2.x does not use the Jupiter Engine but implements its own SpockEngine and its own SpockExecutionContext based on the Jupiter API. Obviously it does not support Jupiter extensions, probably because Spock has its own extension mechanisms for annotation-driven and global extensions.

Bottom line: I recommend to port your JUnit Jupiter extension to a Spock extension. There you also have better means to hook into the Spock lifecycle.

Solution 2:[2]

The thread is old, but in case anyone would be searching for the similar problem, there is a way now to use Junit 5 extensions with Spock 2: spock-junit5.

I did not try it with spring boot extension, but it should work because all required jupiter extension interfaces supported by the library.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kriegaex
Solution 2 xvik