单元测试中JMock的简单使用


单元测试,前期代码准备

<?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>

  业务逻辑部分代码

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;
    }
}

  

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);
}

  

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;
    }
}

  工具类

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;

    }
}

  

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;
        }


    }
}

  单元测试中JMock的简单使用

 

 

单元测试中JMock的简单使用

 

单元测试中JMock的简单使用

 

 单元测试中JMock的简单使用

 

 单元测试中JMock的简单使用

 

 

单元测试中JMock的简单使用

 

 

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);

    }
}

  单元测试中JMock的简单使用

 

 单元测试中JMock的简单使用

 

 单元测试中JMock的简单使用

 

 

单元测试中JMock的简单使用

 

 service类单元测试

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);
    }
}

  

单元测试中JMock的简单使用

 

原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/270879.html

(0)
上一篇 2022年7月1日
下一篇 2022年7月1日

相关推荐

发表回复

登录后才能评论