1

I have set up a new project. The project follows the Maven's default project configuration and naming i.e. I have src->main->java and src->test->java. pom.xml doesn't specify the <sourceDirectory> and <testDirectory> because I have the Maven's default location set up. The pom.xml includes the junit dependecy as

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> 

Project Structure:

enter image description here

I have cleaned and built my project numerous times. However I am not able to import org.junit.Test in my test class which resides in src/test/java/com/.../service/ServiceClassTest.java.

I also have eclipse in my machine and I was able to import the Junit's Test class without changing any config. Why is Intellij failing to import the Junit classes ?

EDIT: For that matter, I am also not able to see Mockito classes inside the test class, although the jar has been downloaded. I can see it in the External Libraries view.

4
  • 1
    This should work. My guess is that you either didn't ask IntelliJ to import all your dependencies, you aren't pointing to a repo that has the JUnit JAR in it, or it doesn't appear under External Libraries in the Project view. It's not IntelliJ; it's you. Commented Feb 5, 2018 at 16:48
  • It appears in the External Libraries. Just checked. Commented Feb 5, 2018 at 16:49
  • I have a project set up in exactly that way. I see this in my JUnit test class: import org.junit.Test; It works. I'm using IntelliJ 2017.2.5. Add the annotation and see what IntelliJ asks you. It should ask to add the import for you if the JAR is in the CLASSPATH. I'm using both JUnit and Mockito. It works if done properly. Commented Feb 5, 2018 at 16:50
  • It asked me to add it to the CLASSPATH. I wonder why ? I mean what did I miss here. Commented Feb 5, 2018 at 16:55

1 Answer 1

3

Go to your parent pom.xml, and re-import it. Make sure IntelliJ understood that you are using a Maven build manager.

By asking to add it to the classpath, it would just add the library to your pom.xml.

Just another question - is your project structure ok? What happens if you hit Build > Build Project ?

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Simon, as mentioned in my previous comments, the import was throwing an error. ALT + Enter on the error asked me to add it to classpath. So the import issue is resolved. But now, when I am writing my test cases, I see that it's not able to import classes from src/main/java. I had to again add "main" to classpath as suggested by Intellij. I now doubt if there is some setup missing on my local Intellij. Because others in my team are able to work on the test cases without any issues.
Just to avoid the mess, after setting up the dependencies in the Pom, delete the .idea/ and the .iml flies and open the project again. This will re-import all Settings from Maven.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.