Run Junit4 tests with version 2016.3.1
Answered
I have a large Maven project that has a dependency to JUnit 4.2.8. In IntelliJ version 2016.2.5 (community edition) I was able to run unit tests in the IDE. But this no longer works with version 2016.3.1. Interestingly, the community edition gives me a different error than the ultimate edition.
Community edition:
org.aspectj.lang.NoAspectBoundException: Exception while initializing com.vmware.nsx.management.container.aspects.TransactionAspect: java.lang.NoSuchMethodException: com.vmware.nsx.management.container.aspects.TransactionAspect.aspectOf()
at org.aspectj.lang.Aspects.aspectOf(Aspects.java:52)
Ultimate Edition:
Dec 15, 2016 5:07:02 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
INFO: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.Preconditions.notEmpty([Ljava/lang/Object;Ljava/lang/String;)[Ljava/lang/Object;
at org.junit.platform.launcher.core.DefaultLauncher.registerTestExecutionListeners(DefaultLauncher.java:71)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:44)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
I have tried to see if there's a way to tell IntelliJ to use a JUnit4 runner, but didn't find it. It's laudable that IntelliJ supports JUnit 5 already, but it will be a while until my team is ready to move away from JUnit 4.
If anyone can give me a hint how to make this work I would be very grateful.
Please sign in to leave a comment.
Hi Mboon,
the community edition: there is no full trace but community edition doesn't include aspectj support plugin so probably that's the cause of the problem
the ultimate edition error: it looks like you have junit 5 in the classpath and thus IDEA delegates the execution to it. And it looks like that you have junit 5 M2 which is not supported anymore for 2016.3.1. You need to downgrade IDEA to 2016.3.0, update junit to M3 in oder to make this work or remove junit 5 from dependencies. Sorry for the inconvenience
Anna
Thank you for looking into this.
With regards to your remark about aspects not being supported by the community edition, I wondered why then it was working with the community edition 6.2.5. I figured out this is because I had first built the project with Maven before trying to run the tests with IntelliJ IDEA. When I do the same with the 6.3 community edition I get the same result, the tests run fine.
It's different with the ultimate edition however. Our Maven POM declares a JUnit dependency for version 4.8.2. But the IDE uses a JUnit5 runner it seems. Even when I add a @RunWith(JUnit4.class) annotation to my test class, it uses JUnit5IdeaTestRunner as can be seen in the message I first posted.
So that's why I was wondering if there's a setting in the ultimate edition where I can tell it which unit test runner to use.
There is no such a setting, if IDE finds junit 5 in the classpath it delegates everything to junit. There is no difference between Community Edition and Ultimate Edition in junit support.
Please try to search e.g. AfterEach junit 5 annotation to see which dependency retrieving it in both cases so you'll see what's different.
Thanks,
Anna
OK, I found that our project has mixed usage of JUnit. My module uses org.junit.vintage, version 4.12.0-M2. Another module uses org.junit.jupiter, version 5.0.0-M2. Apparently Maven has no problem mixing these. But since version 6.3 of IntelliJ, the mere presence of the org.unit.jupiter dependency causes it to automatically use the JUnit5 runner also for the JUnit4 vintage unit tests. If I comment out the jupiter dependency, my vintage unit tests run fine in IntelliJ. But then my Maven build of the whole project breaks...
So it seems to me IntelliJ shouldn't automatically switch to the JUnit5 runner on some, possibly mistaken notion of what runner a test needs. If it can't figure it out properly in every case, there should be an option to select what runner to use in the Run Configuration. Just my humble opinion of course...
When Junit would preserve the compatibility between versions to start, there won't be a problem. For now we decided that if one use non-stable version of junit, than it would be the last public available version. Sorry for the inconvenience.