jay
jay

Reputation: 1524

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.

Upvotes: 8

Views: 10756

Answers (3)

Danke Xie
Danke Xie

Reputation: 1797

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

Upvotes: 1

SparkOn
SparkOn

Reputation: 8946

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.

Upvotes: 1

jay
jay

Reputation: 1524

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.

Upvotes: 1

Related Questions