Maven Junit单元测试入门 小灰灰 2022-05-17 05:24 254阅读 0赞 参考自[敲代码的卡卡罗特][Link 1]、[七夜 雪][Link 2] 1、先新建完成一个基本的maven项目,如[maven 入门例子][maven]。新建功能类包、测试类包,结构如下: ![70][] 2、编写功能类Calculator package com.function; public class Calculator { public int add(int a,int b){ return a+b; } public int subtract(int a,int b){ return a-b; } public int multiply(int a,int b){ return a*b; } public int divide(int a,int b){ return a/b; } } 3、在pom.xml中配置junit依赖包 <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.gan</groupId> <artifactId>mavenFirst</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>mavenFirst</name> <description/> <properties> <webVersion>3.1</webVersion> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 配置单元测试junit的支持包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope><!-- 只在测试时加载包 --> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.2.RELEASE</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project> 4、编写测试基类BaseTest package com.test; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration // enable WebApp Test Support //@ContextConfiguration({"classpath:spring-mvc.xml","classpath:spring-mybatis.xml"}) public class BaseTest { @Before public void init(){ //在运行测试之前的业务代码 } @After public void after(){ //在测试完成值之后的业务代码 } } 5、右键Calculator选择new——other——Junit Test Case ![70 1][] 选择src/test/java文件夹、com.test包,类名默认、添加BaseTest父类 ![70 2][] ![70 3][] ![70 4][] next,勾选需要测试的方法,finish完成 ![70 5][] 6、实现完成测试方法 package com.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import org.junit.Test; import com.function.Calculator; /** * junit的测试方法必须使用 @ Test 注解 * 测试方法必须以public void修饰,并且不包含参数 */ public class CalculatorTest extends BaseTest { @Test public void testAdd() { /** * assertEquals是一个断言方法 * 第一个参数表示预期的结果 * 第二个参数表示程序的执行结果 * 但预期的结果与执行结果一致时,则表示单元测试成功 */ assertEquals(4,new Calculator().add(2,2)); } @Test public void testSubtract() { assertEquals(5,new Calculator().subtract(6, 1)); } @Test public void testMultiply() { assertEquals(8,new Calculator().multiply(2, 4)); } @Test public void testDivide() { assertEquals(1,new Calculator().divide(6, 0)); } } 7、保存,右键Run as——JUnit Test ![70 6][] 可以看到除数为零错误 ![70 7][] [Link 1]: https://www.cnblogs.com/coder-lzh/p/9060110.html [Link 2]: https://www.cnblogs.com/qiyexue/p/6822791.html [maven]: https://blog.csdn.net/zero_130/article/details/81476129 [70]: /images/20220517/f76da51180dd4454b2134699a98bf785.png [70 1]: /images/20220517/2db16a24c96544c0b647190f81462de0.png [70 2]: /images/20220517/404acdfb723e45fdb3e8d91f75ad642a.png [70 3]: /images/20220517/d7e42cc9f602485ea94253c819f6ca27.png [70 4]: /images/20220517/908530f6f564406ea71c47c9d8c8091e.png [70 5]: /images/20220517/ab2610dd17854e9e991391e3a9870998.png [70 6]: /images/20220517/17c45cabd2d74409ba6b7a759669a57f.png [70 7]: /images/20220517/4be361ba435a4c7ca27f28569143c787.png
还没有评论,来说两句吧...