JUnit 5 classes do not auto-import

Answered

I am running IntelliJ 2017.2.1 and trying to start a new project working with JUnit 5. I have my dependencies for JUnit correctly set up in Maven and tests run without issue if I manually write the import statements but for some reason, IntelliJ won't recognise classes as belonging to JUnit in order to import them.

For example, if I write an @Test annotation, IntelliJ will not auto-import (despite the setting being turned on and working for other dependencies) and will not recognised or suggest any matching classes. However, it is fine when I manually write import org.junit.jupiter.api.Test; and tests run as normal.

Is there some IntelliJ configuration I am missing?

1
9 comments

Auto import will not work if there is conflicting import with the same name, you need to use Alt+Enter and choose the specific class you want to import.

0
Avatar
Permanently deleted user

Hi Serge, thanks for the reply.

In this case, there are no conflicting imports (or at least, none displayed). IntelliJ does not recognise any potential classes and using Alt+Enter suggests to add a Maven dependency. Interestingly, even if I add the JUnit dependency via the "Add Maven Dependency..." option, it still doesn't recognise any matching classes to import.

1

Please file a bug at https://youtrack.jetbrains.com/issues/IDEA with the sample project to reproduce attached.

0
Avatar
Permanently deleted user

Frustratingly, I've created a sample and cannot replicate the issue in a fresh project. Is there any other diagnostics you could suggest?

0

Try creating a new project from scratch on top of your existing sources. If it's Maven/Gradle based, perform the reimport. Please try File | Invalidate Caches | Invalidate and Restart.

0
Avatar
Permanently deleted user

Thanks Serge. I created a new project from my git repo and it looks like the new project doesn't suffer from the same issue.

I had previously tried reimporting all sources and invalidating caches but neither worked.

1
Avatar
Permanently deleted user

I have the exact same issue. I have removed Junit 4.12 and added JUnit 5.4.0.

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>

After removing the old JUnit 4 import for @Test, I get the same options like Sam, add testng to classpath, create annotation or type parameter and add maven dependency. I tried that option for the sake of trying and see what happens. I get a new entry in my pom.xml's dependencies but the imports are still NOT working. I have tried Invalidate Caches & Restart, no luck.

 

Later edit: Found the issue, maybe somebody else ran into this. In order to avoid seeing org.junit.Test as import suggestion for @Test, I have excluded org.junit for import suggestions. This also EXCLUDES Junit 5, as it also starts with org.junit.

0

If the problem is with the maven import and you don't see the jars under the  External Libraries node, you need to investigate why the import fails by checking the logs, see https://stackoverflow.com/a/42427510/104891.

0
Avatar
Permanently deleted user

@Serge, see my edit to my previous post and, if you have rights, you can delete this comment and yours so we can keep the thread clean and let my edited comment so others can find a solution if they had the same issue.

0

Please sign in to leave a comment.