Intellij does not find source files from test directory

I am new to Intellij and have 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 cannot resolve the App symbol in AppTest.java . In the project structure, MyProject content root for the MyProject module MyProject , src/main installed in the root content directory for the main module, and src/test installed in the project root for the test module.

I tried the solution here: Add main / java classes to my test / java directory in intellij , but Intellij did not allow me to add src/main as a dependency of src/test . This tells me: the MyProject module should not contain the original root "blah / blah / src / main / java". The root already belongs to the "main" module.

Can someone please tell me how to configure my modules / project so that Intellij can find all classes? Thanks.

+6
source share
2 answers

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

+1
source

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

+1
source

Source: https://habr.com/ru/post/971297/


All Articles