执行mvn clean test(JUnit)时无法找到编译类及相关错误求助
Maven+JUnit测试编译失败问题修复
项目结构
│ .classpath │ .project │ LICENSE.txt │ NOTICE.txt │ pom.xml │ README.txt ├───site └───src ├───main │ ├───java │ │ └───com │ │ └───daydreamer │ │ └───app │ │ TestApp.java └───test ├───java │ └───com │ └───daydreamer │ └───app │ TestAppTest.java
POM配置
<?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.daydreamer.app</groupId> <artifactId>testpkg</artifactId> <version>1</version> <properties> <maven.compiler.release>11</maven.compiler.release> <junit.version>5.8.2</junit.version> </properties> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
主代码(TestApp.java)
package com.daydreamer.app; public class TestApp { // Fields private int a; // Constructor public TestApp(int a) { this.a = a; } // Getters public int getA() { return this.a; } // String representation @Override public String toString() { String strRepr = "TestApp {" + this.a + "}"; return strRepr; } }
测试代码问题与错误信息
原始测试代码(TestAppTest.java)
import com.daydreamer.app.TestApp; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class TestAppTest { @Test void getATest0() { var expected = 1; var actual = TestApp(1).getA(); Assertions.assertEquals(actual, expected); } @Test void toStringTest1() { var expected = "1"; var actual = TestApp(1).toString(); Assertions.assertEquals(actual, expected); } }
执行mvn clean test报错
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:testCompile (default-testCompile) on project testpkg: Compilation failure: Compilation failure: [ERROR] ./src/test/java/com/daydreamer/app/TestAppTest.java:[11,22] cannot find symbol [ERROR] symbol: method TestApp(int) [ERROR] location: class TestAppTest [ERROR] ./src/test/java/com/daydreamer/app/TestAppTest.java:[18,22] cannot find symbol [ERROR] symbol: method TestApp(int) [ERROR] location: class TestAppTest [ERROR] -> [Help 1]
LSP提示
the declared package "" does not match the expected package "com.daydreamer.app"
修复方案
1. 修正对象实例化语法
Java中实例化对象必须使用new关键字调用构造方法,把TestApp(1)改为new TestApp(1)。
2. 添加正确的包声明
测试类位于com.daydreamer.app目录下,必须添加对应的包声明package com.daydreamer.app;,否则编译器会将其归为默认包,导致包不匹配错误。
3. 修正断言预期值
TestApp的toString方法返回格式为TestApp {1},而非原始代码中的"1",需要同步修正测试断言的预期值,避免测试执行失败。
修复后的完整测试代码
package com.daydreamer.app; import com.daydreamer.app.TestApp; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class TestAppTest { @Test void getATest0() { var expected = 1; var actual = new TestApp(1).getA(); Assertions.assertEquals(actual, expected); } @Test void toStringTest1() { var expected = "TestApp {1}"; var actual = new TestApp(1).toString(); Assertions.assertEquals(actual, expected); } }
验证修复
执行mvn clean test,编译和测试都会正常通过,LSP的包错误提示也会消失。
内容的提问来源于stack exchange,提问作者DDStackoverflow
相关产品推荐
相关产品推荐

