单元测试中JMock的简单使用
单元测试,前期代码准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.2 . 1 .RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.</groupId> <artifactId>jmockit</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>jmockit</name> <description>Demo project for Spring Boot</description> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 先声明jmockit的依赖 --> <dependency> <groupId>org.jmockit</groupId> <artifactId>jmockit</artifactId> <version> 1.36 </version> <scope>test</scope> </dependency> <!-- 再声明junit的依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.9 </version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version> 4.1 </version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version> 3.9 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
业务逻辑部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package com.jmockit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class JmockitApplication { public static void main(String[] args) { SpringApplication.run(JmockitApplication. class , args); } } package com.jmockit; import java.util.Map; /** * @author Farben * @version 1.0 * @date 2022/6/30 21:29 */ public interface UserMapper { Map<String,Object> getMap(); } package com.jmockit; import java.util.HashMap; import java.util.Map; /** * @author Farben * @version 1.0 * @date 2022/6/30 21:39 */ public class UserMapperImpl implements UserMapper{ @Override public Map<String, Object> getMap() { Map<String, Object> result= new HashMap<>(); result.put( "name" , "小六子" ); result.put( "sex" , "男" ); result.put( "age" , "18" ); result.put( "address" , "杭州" ); return result; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.jmockit; import java.util.Map; /** * @author Farben * @version 1.0 * @date 2022/6/30 21:25 */ public interface UserService { Map<String,Object> test(String name,String pwd); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.jmockit.service.impl; import com.jmockit.UserMapper; import com.jmockit.UserService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; /** * @author Farben * @version 1.0 * @date 2022/6/30 21:25 */ @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Map<String, Object> test(String name, String pwd) { Map<String, Object> resultMap= new HashMap<>(); if (StringUtils.isEmpty(name)){ return resultMap; } Map<String, Object> map = userMapper.getMap(); map.put( "pwd" , "123" ); return map; } } |
工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.jmockit; import java.util.HashMap; import java.util.Map; /** * @author Farben * @version 1.0 * @date 2022/6/30 21:23 */ public class Utils { public static Map<String,Object> getUser(){ Map<String,Object> param= new HashMap<>(); param.put( "pwd" , "123" ); return param; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package com.jmockit.controller.front; import com.jmockit.UserService; import com.jmockit.Utils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * @author Farben * @version 1.0 * @date 2022/6/30 21:15 */ public class TestController { @Autowired private UserService userService; @RequestMapping ( "/queryName" ) @ResponseBody public Map<String, Object> queryName( @RequestBody Map map){ Map<String, Object> result= new HashMap<>(); try { String name = MapUtils.getString(map, "name" ); if (StringUtils.isEmpty(name)){ result.put( "msg" , "名字为空" ); return result; } Map<String, Object> user = Utils.getUser(); String pwd = user.get( "pwd" ).toString(); Map<String, Object> resultMap = userService.test(name, pwd); return resultMap; } catch (Exception e) { System.out.println( "异常打印" ); return result; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | package com.jmockit.controller.front; import com.jmockit.UserService; import com.jmockit.Utils; import mockit.Expectations; import mockit.Injectable; import mockit.Mock; import mockit.Tested; import mockit.integration.junit4.JMockit; import org.junit.Test; import org.junit.runner.RunWith; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.*; @RunWith (JMockit. class ) public class TestControllerTest { @Tested private TestController testController; @Injectable private UserService userService; @Test public void testException() { Map<String,Object> param= new HashMap<>(); param.put( "name" , "test1992" ); Map<String,Object> user= new HashMap<>(); user.put( "pwd" , "999" ); String name= "test1992" ; String pwd= "999" ; Map<String, Object> map = new HashMap<>(); map.put( "address" , "武汉" ); new Expectations(Utils. class ){ { Utils.getUser(); result=user; } }; new Expectations(){ { userService.test(name, pwd); result= new Exception( "异常" ); } }; testController.queryName(param); } @Test public void testEmpty() { Map<String,Object> param= new HashMap<>(); param.put( "name" , "" ); testController.queryName(param); } @Test public void queryName() { Map<String,Object> param= new HashMap<>(); param.put( "name" , "test1992" ); Map<String,Object> user= new HashMap<>(); user.put( "pwd" , "999" ); String name= "test1992" ; String pwd= "999" ; Map<String, Object> map = new HashMap<>(); map.put( "address" , "武汉" ); new Expectations(Utils. class ){ { Utils.getUser(); result=user; } }; new Expectations(){ { userService.test(name, pwd); result=map; } }; testController.queryName(param); } } |
service类单元测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.jmockit.service.impl; import com.jmockit.UserMapperImpl; import com.jmockit.Utils; import mockit.Expectations; import mockit.Injectable; import mockit.Tested; import mockit.integration.junit4.JMockit; import org.junit.Test; import org.junit.runner.RunWith; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.*; @RunWith (JMockit. class ) public class UserServiceImplTest { @Tested private UserServiceImpl userService; @Injectable private UserMapperImpl userMapper; @Test public void testParam() { String name= "" ; String pwd= "" ; userService.test(name,pwd); } @Test public void testMethod() { String name= "admin" ; String pwd= "111" ; Map<String,Object> param= new HashMap<>(); param.put( "pwd" , "123" ); new Expectations(){ { userMapper.getMap(); result=param; } }; userService.test(name,pwd); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异