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.
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.
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.
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.
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.
Just add the main module as a dependency of the test module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With