在执行 mvn clean test 时报错:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project hello: Compilation failure: Compilation failure: [ERROR] /D:/mavenWork/src/test/java/com/clc/helloworld/test/TestHelloworld.java:[2,17] 程序包org.junit不存在 [ERROR] /D:/mavenWork/src/test/java/com/clc/helloworld/test/TestHelloworld.java:[4,10] 找不到符号
但是执行 mvn clean compile 是没问题的 ,解决办法(更改junit的依赖):
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
改为(删去 <scope>test</scope>):
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
摘自:https://blog.csdn.net/weixin_38423249/article/details/79777885
最终解决方案:
1、Maven 项目中,测试用例相关代码要按规定放到 src/test/java 目录中;
2、org.junit.Assert 类提供了很多用于测试的静态方法。失败的断言将导致一个异常被抛出,并将终止当前正在执行中的测试。导入这些断言的最高效的方式是通过一个 import static 语句来实现:
import static org.junit.Assert.*;
一旦这样做了,就可以直接调用 Assert 方法了,如:
assertEquals(buf.readSlice(3), read);