Unit test - spring boot resume unit test

In the Spring project, unit test items need to be established for each layer of controller, service and dao. For different layers, we can use junit and mock in different ways.

SpringBoot increases dependency

	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test<artifactId>
        <scope>test</scope>
    </dependency>

Build test base class

  • New JUnitBaseTest.java
package com.lucas.device;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.util.StopWatch;

import lombok.extern.slf4j.Slf4j;

/**
 * <Description> <br>
 * 
 * @author xubin<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018 September 27, 2007 < br >
 */

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class JUnitBaseTest {

    /**
     * sw Timer < br >
     */
    public static StopWatch sw = null;

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     * @throws java.lang.Exception <br>
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        sw = new StopWatch();
    }

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     * @throws java.lang.Exception <br>
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        log.info("**************************************************************");
        log.info("Unit test timing statistics:{}", sw.prettyPrint());
        log.info("**************************************************************");
    }

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     * @throws java.lang.Exception <br>
     */
    @Before
    public void setUp() throws Exception {
        log.info("Start testing-----------------");
    }

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     * @throws java.lang.Exception <br>
     */
    @After
    public void tearDown() throws Exception {
        sw.stop();
        log.info("End of test-----------------");
    }
}

  • @BeforeClass trigger before base class execution
  • @Triggered after the execution of the AfterClass base class
  • @Before Trigger before every @ Test method call
  • @After every @ Test method call is triggered

Test Case

  • service layer unit test sample DeviceInfoTest.java
package com.lucas.device.service;

import static org.junit.Assert.fail;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.lucas.core.common.PageInfo;
import com.lucas.device.JUnitBaseTest;
import com.lucas.device.bo.DeviceInfoBO;
import com.lucas.device.entity.DeviceInfoEntity;
import com.lucas.device.entity.cache.DeviceCache;
import com.phlico.common.framework.tool.unique.IdWorkerUtil;
import com.phlico.common.framework.web.util.JsonUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * <Description> <br>
 * 
 * @author xubin<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate Nov 4, 2018 <br>
 */

@Slf4j
public class DeviceInfoTest extends JUnitBaseTest {

    /**
     * deviceInfoService <br>
     */
    @Resource(name = "deviceInfoServiceImpl2")
    private DeviceInfoService deviceInfoService;

    /**
     * deviceRedisTemplate <br>
     */
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * appKeyArr <br>
     */
    public static final String[] appKeyArr = {
            "2e2b380a56e7464aa678294c2c345545", "e2c85a1d245844fd9bdb14f0d0fc868a", "fa58f5306eb64ce094fe65fec6261587"
    };

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     *         <br>
     */
    @Ignore
    @Test
    public void saveTest() {
        log.info("Equipment preservation");
        sw.start("Equipment preservation");
        try {
            DeviceInfoBO deviceInfoBO = new DeviceInfoBO();
            deviceInfoBO.setAppKey("2e2b380a56e7464aa678294c2c345545");
            deviceInfoBO.setDeviceName("test2");
            deviceInfoBO.setDeviceCode("EE-FF-GG");
            deviceInfoBO.setDeviceDesc("Test equipment 1");
            deviceInfoBO.setDeviceType("bcb0ca4ef0eb463aa76a5bc0ccee1b85");
            deviceInfoBO.setDeviceState("b9cf9e01a5534487919a77f21c34c93c");
            deviceInfoBO.setDeviceIpv4("192.168.109.12");
            deviceInfoBO.setDevicePort(8088);
            deviceInfoBO.setDeviceMac("ED:AS:ZQ:QS");
            deviceInfoBO.setProtocol(0);
            deviceInfoService.save(deviceInfoBO);
        } catch (Exception e) {
            fail("Device save exception");
        }
    }

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     *         <br>
     */
    @Ignore
    @Test
    public void updateTest() {
        log.info("Equipment modification");
        sw.start("Equipment modification");
        try {
            DeviceInfoBO deviceInfoBO = new DeviceInfoBO();
            deviceInfoBO.setAppKey("2e2b380a56e7464aa678294c2c345545");
            deviceInfoBO.setDeviceName("Test equipment-" + appKeyArr[(int) (Math.random() * appKeyArr.length)]);
            deviceInfoBO.setDeviceCode("EE-FF-GG");
            deviceInfoBO.setDeviceDesc("Test equipment 2");
            deviceInfoBO.setDeviceState("b9cf9e01a5534487919a77f21c34c93c");
            deviceInfoBO.setDeviceIpv4("192.168.109.122");
            deviceInfoBO.setDevicePort(8089);
            deviceInfoBO.setDeviceMac("ED:AS:ZQ:QS:DA");
            deviceInfoBO.setProtocol(0);
            deviceInfoService.update(deviceInfoBO);
        } catch (Exception e) {
            fail("Equipment modification exception");
        }
    }

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     *         <br>
     */
    @Ignore
    @Test
    public void delDeviceByCodesTest() {
        log.info("Device deletion");
        sw.start("Device deletion");
        try {
            deviceInfoService.delDevicesByCode("2e2b380a56e7464aa678294c2c345545", "EE-FF-GG", null, null, null);
        } catch (Exception e) {
            fail("Device deletion exception");
        }
    }

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     *         <br>
     */
    @Ignore
    @Test
    public void delDeviceByIdsTest() {
        log.info("Press id Delete device");
        sw.start("Press id Delete device");
        try {
            deviceInfoService.delete("2e2b380a56e7464aa678294c2c345545", "d99b77637c1e421db89ac89579d43eef,00d2105433d94106a88596f275e58d41", null,
                    null, new Date());
        } catch (Exception e) {
            fail("Press id Delete device exception");
        }
    }

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     *         <br>
     */
    @Ignore
    @Test
    public void queryEntityListByPageTest() {
        log.info("Paging query device");
        sw.start("Paging query device");
        try {
            DeviceInfoBO bo = new DeviceInfoBO();
            bo.setAppKey("2e2b380a56e7464aa678294c2c345545");
            PageInfo<DeviceInfoBO> list = deviceInfoService.queryEntityListByPage(bo, 1, 15);
            log.info(JsonUtil.getSucc4data(list));
            Assert.assertNotEquals(null, list);
        } catch (Exception e) {
            fail("Paging query device exception");
        }
    }

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
     *         <br>
     */
    @Ignore
    @Test
    public void queryDeviceInfoByCacheTest() {
        log.info("Query cache all devices");
        sw.start("Query cache all devices");
        try {
            String appKey = "fa58f5306eb64ce094fe65fec6261587";
            String deviceCodes = "577984734685110272";
            List<DeviceCache> list = deviceInfoService.queryDeviceInfoByCache(appKey, deviceCodes);
            log.info(JsonUtil.getSucc4data(list));
            Assert.assertNotEquals(null, list);
        } catch (Exception e) {
            fail("Query cache all device exceptions");
        }
    }
}
  • @Ignore test method
  • @Test the method of executing unit test

Use StopWatch to count the total execution time of unit Test classes and the execution time of each @ Test method.

Tags: Programming Junit Java Spring Lombok

Posted on Tue, 10 Dec 2019 02:03:45 -0800 by zachrose