Cannot resolve symbol 'Test' 解决办法 Intellij Idea 的maven工程在sources目录中使用 @Test

这两天一直在适应新的开发工具IDEA ,今天在使用maven写web项目时遇到一个问题,查了许久,我把解决的方法总结一下,希望看到这篇文章的朋友能快速找到问题所在。

问题描述:

intellij的maven工程,在sources目录中的一个java文件中,使用junit的@Test 注释类,但是intellij提示找不到类 cannot resolve symbol ‘Test’。而查看自己的pom文件,已经引入了junit包,该注释类在maven的依赖包中是存在的。

方法一: 
将pom文件中 junit的引用设置scope为compile 

junit 
junit 
4.10 
compile 

这里还有个问题,一般jar包的默认作用域都是compile。难道junit这个包的默认作用域是test。

方法二: 
打开工程结构 File>Project Structure 
选择目标工程, 在右边栏中点击 Dependencies, 将unit的scope设置为Compile。 
Cannot resolve symbol 'Test' 解决办法 Intellij Idea 的maven工程在sources目录中使用 @Test

方法三:打开工程结构 File>Project Structure >modules
选择目标工程, 在右边栏中点击 Dependencies, 此时看到项目没有一个依赖时,要注意了:

 当你创建一个新的项目时,在idea编辑器的右下角会出现如下的提示,

Cannot resolve symbol 'Test' 解决办法 Intellij Idea 的maven工程在sources目录中使用 @Test

Maven projects need to be imported 

Import Changes Enable Auto-Import。点击Enable Auto-Import,以后更改pom文件后就会自动下载依赖包了,此时再看目标工程,点击Dependencies,出现方法二的界面即可使用@Test

Select this checkbox, if you want IntelliJ IDEA to perform reimport automatically each time you change your pom.xml