Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()
like image 946
Aethernite Avatar asked Oct 27 '25 04:10

Aethernite


1 Answers

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.

like image 164
Leonard Brünings Avatar answered Oct 29 '25 20:10

Leonard Brünings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!