java

单元测试 & EasyMock

2019-08-25  本文已影响0人  suxin1932

1.单元测试

1.1 使用testng & mockito进行单元测试

pom.xml

<?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 http://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.1.6.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.zy.netty</groupId>
    <artifactId>spring-boot-netty-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-netty-demo</name>
    <description>Demo project for Spring Boot netty</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>
        </dependency>

        <!-- mockito-core + testng 进行单元测试, 覆盖各分支 -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
package com.zy.netty.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {

    public String hello() {
        return "service --> hello";
    }
}
package com.zy.netty.controller;

import com.zy.netty.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @RequestMapping("hello")
    public String hello(Integer no) {
        if (Objects.isNull(no)) {
            return "controller --> hello";
        }
        return helloService.hello();
    }

    private void world(String world, boolean flag) {
        if (flag) {
            System.out.println("-------------");
        } else {
            System.out.println(String.format("controller --> %s", world));
        }
    }

}

测试类

package com.zy.netty.controller;

import com.zy.netty.service.HelloService;
import org.mockito.*;
import org.springframework.util.ReflectionUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * https://sunxiang0918.cn/2016/03/28/%E4%BD%BF%E7%94%A8Mockito%E5%92%8CSpringTest%E8%BF%9B%E8%A1%8C%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/
 */
public class HelloControllerTest {

    @InjectMocks
    private HelloController helloController;

    @Mock
    private HelloService helloService;

    @BeforeTest
    public void setUp() {
        // 这句话就是对所有标注了@Mock注解的对象进行模拟
        MockitoAnnotations.initMocks(this);
    }

    @Test
    /**
     * 测试 public 方法
     */
    public void testHello() {
        // 分支一
        String hello = helloController.hello(null);
        System.out.println(hello);
        // 分支二
        // 这句话意思是: 无论何时, 只要调用了 helloService.hello() 方法, 都返回 world
        BDDMockito.when(helloService.hello()).thenReturn("world");
        String hello1 = helloController.hello(1);
        System.out.println(hello1);
    }

    @Test
    /**
     * 测试 private 方法:
     *  运用反射
     */
    public void testWorld() {
        Method world = ReflectionUtils.findMethod(HelloController.class, "world", String.class, boolean.class);
        world.setAccessible(true);
        // 分支一
        try {
            world.invoke(helloController, "world1", true);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        // 分支二
        try {
            world.invoke(helloController, "world2", false);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

2.EasyMock

http://easymock.org/

上一篇下一篇

猜你喜欢

热点阅读