IDEA中用junit写基本测试用例

前提:使用Maven管理依赖

参考文档:

首先在pom.xml中增加junit依赖,然后刷新Maven

<!-- junit依赖 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>

在 test/java 下新建包和java文件

写一个方法,方法上一行加上 @TEST 注解

在方法内部使用 assertEquals() ,并导入对应文件  import static org.junit.Assert.assertEquals; 

直接使用,第一个参数是实际执行的表达式 ,第二个参数是期望值:

 assertEquals()  可以有多个,如果实际值和期望值相等就通过测试,否则不通过测试。

如果全都通过测试,显示绿色对号:

如果有未通过的测试用例,会提示错误,代码中错误的用例也会标红下划线:

posted @ 2024-07-22 09:34  sunshine233  阅读(3)  评论(0编辑  收藏  举报