Junit单元测试实例
1. 在 IDEA 中打开 File -> New -> Project。
2. 在左侧的菜单栏中选择 Java ,Name 是项目名称,Location 是项目的位置,JDK 根据个人项目的情况选择。
3. 下载对应的 Mockito 插件,我下载的插件是 Mockito Postfix Completion 。
4. 配置 Maven 设置
5. 选中项目的根目录,在 Help 中选择 Find Action,输入 Add framework support 找到对应的指令。
6. 勾选 Maven 选项,点击 OK。(如果项目报错可以尝试重新加载项目)
7. 在 pom 文件中添加 Junit 依赖。

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>groupId</groupId> 8 <artifactId>JunitTestCase1</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <maven.compiler.source>11</maven.compiler.source> 13 <maven.compiler.target>11</maven.compiler.target> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 <!-- 在这里添加项目依赖 --> 19 <dependency> 20 <groupId>junit</groupId> 21 <artifactId>junit</artifactId> 22 <version>4.13.2</version> 23 <scope>test</scope> 24 </dependency> 25 <dependency> 26 <groupId>org.apache.commons</groupId> 27 <artifactId>commons-lang3</artifactId> 28 <version>3.12.0</version> <!-- 确保使用最新版本 --> 29 </dependency> 30 </dependencies> 31 </project>
8. 创建一个Java类,用来检验 IPV4,MAC,Port 的格式,代码如下:

1 import org.apache.commons.lang3.StringUtils; 2 3 import java.util.regex.Pattern; 4 5 public class StringValidation { 6 7 /** 8 * IPv4 pattern string. 9 */ 10 private static final String IPV4_PATTERN_STRING = "^(?:(1\\d\\d\\.)|(2[0-4]\\d\\.)|(25[0-5]\\.)|([1-9]\\d\\.)|(\\d\\.)){3}(?:(1\\d\\d)|(2[0-4]\\d)|(25[0-5])|([1-9]\\d)|(\\d))$"; 11 12 /** 13 * MAC pattern string. 14 */ 15 private static final String MAC_PATTERN_STRING = "([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}"; 16 17 /** 18 * Port pattern string (0-65535). 19 */ 20 private static final String PORT_PATTERN_STRING = "^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$"; 21 22 /** 23 * IPv4 pattern. 24 */ 25 private static final Pattern IPV4_PATTERN = Pattern.compile(IPV4_PATTERN_STRING); 26 27 /** 28 * Mac pattern. 29 */ 30 private static final Pattern MAC_PATTERN = Pattern.compile(MAC_PATTERN_STRING); 31 32 /** 33 *Port pattern. 34 */ 35 private static final Pattern PORT_PATTERN = Pattern.compile(PORT_PATTERN_STRING); 36 37 /** 38 * Determine if the string is IPv4 address. 39 * 40 * @param ip 41 * IP 42 * @return true if the string is IPv4 address 43 */ 44 public boolean isIPv4(String ip) { 45 if (StringUtils.isBlank(ip)) { 46 return false; 47 } 48 return IPV4_PATTERN.matcher(ip).find(); 49 } 50 51 /** 52 * Determine if the string is MAC address. 53 * 54 * @param mac 55 * MAC address 56 * @return true if the string is MAC address 57 */ 58 public boolean isMac(String mac) { 59 if (StringUtils.isBlank(mac)) { 60 return false; 61 } 62 return MAC_PATTERN.matcher(mac).find(); 63 } 64 65 /** 66 * Determine if the string is port. 67 * 68 * @param port 69 * port 70 * @return true if the string is port 71 */ 72 public boolean isPort(String port) { 73 if (StringUtils.isBlank(port)) { 74 return false; 75 } 76 return PORT_PATTERN.matcher(port).find(); 77 } 78 }
9. 在对应的 Java 类中点击鼠标右键,点击 Generate...,然后点击 Test... 生成单元测试。
10. 因为 Maven pom 文件中导入的依赖是 JUnit4,所以这里选择JUnit4,根据实际的情况勾选以下的复选框,代码如下。

1 import org.junit.After; 2 import org.junit.Before; 3 import org.junit.Test; 4 5 import static org.junit.Assert.*; 6 7 public class StringValidationTest { 8 9 private StringValidation strValidation; 10 11 @Before 12 public void setUp() { 13 // 在每个测试之前执行:初始化资源 14 strValidation = new StringValidation(); 15 } 16 17 @After 18 public void tearDown() { 19 // 在每个测试结束后执行:释放资源 20 strValidation = null; 21 } 22 23 @Test 24 public void isIPv4() { 25 assertTrue(strValidation.isIPv4("127.0.0.1")); 26 assertFalse(strValidation.isIPv4("not an ip address")); 27 } 28 29 @Test 30 public void isMac() { 31 assertTrue(strValidation.isMac("8F:97:F1:41:49:E2")); 32 assertFalse(strValidation.isMac("not a mac address")); 33 } 34 35 @Test 36 public void isPort() { 37 assertTrue(strValidation.isPort("80")); 38 assertFalse(strValidation.isPort("not a port")); 39 } 40 }
如果执行的时候报错 "package org.junit does not exist",可以检查 pom 文件中的依赖是否正确,然后 Reload Project,如果仍然报错,可以尝试点击右键 src/test/java
目录并选择 Mark Directory as -> Test Sources Root,确保该目录被标记为测试源代码目录。