Skip to main content
 首页 » 编程设计

intellij-idea之IntelliJ 无法为 Maven 项目解析 Junit

2025年12月25日34lyhabc

我能在 SO 上找到的最接近的帖子是:Why am I getting "Cannot resolve symbol"?

但这对我没有帮助。 :(

这是我的错误截图:
commercial photography locations

而且我已经确保我的 pom.xml 的范围是,我的实际代码也放在 test 文件夹下。
这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
 
    <groupId>com.fishercoder</groupId> 
    <artifactId>leetcode-algorithms</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-compiler-plugin</artifactId> 
                <configuration> 
                    <source>1.7</source> 
                    <target>1.7</target> 
                </configuration> 
            </plugin> 
        </plugins> 
    </build> 
 
    <dependencies> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
            <version>4.12</version> 
            <scope>test</scope> 
        </dependency> 
    </dependencies> 
</project> 


mvn clean compile 

运行成功。

这是结果
mvn test 

这里:
------------------------------------------------------- 
 T E S T S 
------------------------------------------------------- 
Running com.stevesun.AddBinaryTest 
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.042 sec 
Running com.stevesun.MissingRangesTest 
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec 
 
Results : 
 
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0 
 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.913 s 
[INFO] Finished at: 2017-01-08T09:13:22-08:00 
[INFO] Final Memory: 16M/308M 
[INFO] ------------------------------------------------------------------------ 

我猜我的 IntelliJ 设置有点奇怪?

请您参考如下方法:

编辑 - 通过评论解决:
在您的屏幕截图中,您的测试文件夹中有一个 test.iml。也许有排除或什么?您可以尝试删除该文件吗?

您可以尝试使 IntelliJ 中的缓存无效(通过 File | Invalidate Cache )。

您还可以尝试从 IDE 中重建您的项目(通过 Build | Rebuild Project )。

您还可以尝试通过 Maven 同步项目(重新导入所有 Maven 项目)(右侧某处应该有一个 Maven 选项卡或通过将 Maven 项目输入 Ctrl + Shift + A )。