Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij not finding source files from test directory

I am pretty new to Intellij and just started my first Maven project. I have the following directory structure:

MyProject
├── myapp.iml
├── pom.xml
└── src
    ├── main (sources root)
    │   ├── java
    │   │   └── com
    │   │       └── mysite
    │   │           └── myapp
    │   │               └── App.java
    │   └── main.iml
    └── test (test sources root)
        ├── java
        │   └── com
        │       └── mysite
        │           └── myapp
        │               └── AppTest.java
        └── test.iml

The problem is that the Intellij compiler can't resolve symbol App in AppTest.java. In the Project Structure, I have MyProject set to the content root for the module MyProject, src/main set to the content root for the module main, and src/test set to the project root for the module test.

I tried the solution here: Add main/java classes to my test/java directory in intellij but Intellij wouldn't let me add src/main as a dependency of src/test. It tells me: Module MyProject must not contain source root "blah/blah/src/main/java". The root already belongs to module "main".

Can someone please advise how to set up my modules/project so that Intellij can find all of the classes? Thanks.

like image 208
jay Avatar asked Jun 24 '14 15:06

jay


People also ask

How do I mark a directory as test sources root in IntelliJ?

In the Project tool window ( Alt+1 ), create a new directory in which you will store your test code. Right-click the new directory and select Mark Directory As | Test Sources Root.

How do I add generated sources to IntelliJ?

You can just change the project structure to add that folder as a "source" directory. Project Structure → Modules → Click the generated-sources folder and make it a sources folder.


3 Answers

One easy way to solve this: First, create the test class in the test directory. When you initialize the class to be tested inside testClass, it will show a compiler error. Keep the cursor there and press ALT+ENTER, then, from the pop-up menu, select create class. It's done.

like image 176
SparkOn Avatar answered Oct 14 '22 07:10

SparkOn


Another thing I discovered is that Intellij created for MyProject, main, and test. Getting rid of the main and test modules also seems to solve the problem with no noticeable external side effects. Yet.

like image 29
jay Avatar answered Oct 14 '22 06:10

jay


Just add the main module as a dependency of the test module.

like image 27
Danke Xie Avatar answered Oct 14 '22 08:10

Danke Xie