Home > OS >  @Rule annotation is not considered in spock unit tests using Junit5
@Rule annotation is not considered in spock unit tests using Junit5

Time:09-22

I'm trying to migrate my project to use Junit5. So far I've been using a class "LogSpy" that basically intercepts and saves all the logs so they can be tested easily. Using Junit4 and Spock test I was able to initialize my log interceptor class by using the @Rule annotation (even though it is in a Spock test). After migrating to Junit5 this annotation doesn't seem to initialize the needed log interceptor class and I can't find the reason why. Why did this happen? What are the differences between Junit4 and 5 regarding the @Rule annotation? Is there a way around this issue?

This is how I initialize the LogSpy class. It initializes in JUnit unit tests but not in Spock tests.

@Rule
public LogSpy logSpy = new LogSpy()

CodePudding user response:

From the release notes

JUnit 4 Rules are not supported by spock-core anymore, however, there is a new spock-junit4 module that provides best effort support to ease migration.

In short add the spock-junit4 dependency, if you still need to use JUnit 4 rules.

In the long term, I would suggest to migrate to Spock native extensions.

  • Related