断言

断言(assertions)是测试方法中的核心部分,用来对测试需要满足的条件进行验证。这些断言方法都是 org.junit.jupiter.api.Assertions 的静态方法。JUnit 5 内置的断言可以分成如下几个类别:

1、简单断言

在这里插入图片描述

2、数组断言

通过 assertArrayEquals 方法来判断两个对象或原始类型的数组是否相等

配置类中

也可以设置异常报文,第一个参数是期望值,第二个参数是实际值

@Test
@DisplayName("数组断言测试")
void testArrayAssertions(){
    Assertions.assertArrayEquals(new int[]{1,2},new int[]{1,2},"元素不一样");
}

详细步骤:

Spring boot目录结构一般是固定的:
在这里插入图片描述
选中要测试的方法体右键:
在这里插入图片描述
会弹出新建测试类的框
在这里插入图片描述
上面一个是我测试建的,如果要新建应该选第二个:Create New Test…
在这里插入图片描述
上面是生成测试类的名称以及测试的一些属性,下面是选择你要放入的待测试的模块:

下面开始上代码:

Spring boot自带的测试基类(启动配置):

package com.ai.rai.group.workflow.application;
 
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
/**
 * Project:bean_generator
 * Author:hangke
 * Date:2017/1/13
 * Description://TODO add description here
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class BaseTestApplication
{
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
}

测试类启动入口(一般都有的,这边也贴一下吧):

package com.ai.rai.group.workflow.application;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
 
/**
 * Project:html_template
 * Author:hangke
 * Date:2017/3/22
 * Description://TODO add description here
 */
@SpringBootApplication
@ComponentScan(basePackages={"com.ai", "com.asiainfo"})
public class TestApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(TestApplication.class,args);
    }
}

待测试的Service方法就不贴了,根据自己的实际情况写逻辑就行;

测试类(使用依赖注入):

package com.ai.rai.group.workflow.service.impl;
 
import com.ai.rai.group.workflow.application.BaseTestApplication;
import com.ai.rai.group.workflow.entity.beans.TmWorkInst;
import com.ai.rai.group.workflow.service.IWorkFlowService;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sun.tools.jar.Main;
 
import java.util.List;
 
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
public class WorkFlowServiceImpllswTest extends BaseTestApplication {
 
    //Service里面Dao用了依赖注入,这便不能用new对象的方式
    @Autowired
    IWorkFlowService workFlowServiceImpl;
 
    @Test
    public void selectWorkInstById() {
        //这个用的之前写好的Service测试,
        TmWorkInst workInst = new TmWorkInst();
        List<TmWorkInst> ss = workFlowServiceImpl.selectWorkInstById(workInst);
        for (TmWorkInst o : ss)
            System.out.println("==============="+o.toString());
    }
}

控制台部分信息打印:
在这里插入图片描述

加断言

正常的单元测试流程还需要加断言打印日志

加断言可以避免对数据库的误操作,以及缩短测试流程(断言失败就不再执行之后的代码了)

代码:

package com.ai.rai.group.workflow.service.impl;
 
import com.ai.rai.group.workflow.application.BaseTestApplication;
import com.ai.rai.group.workflow.entity.beans.TmWorkInst;
import com.ai.rai.group.workflow.service.IWorkFlowService;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sun.tools.jar.Main;
 
import java.util.List;
 
import static org.junit.Assert.*;
 
@RunWith(SpringJUnit4ClassRunner.class)
public class WorkFlowServiceImpllswTest extends BaseTestApplication {
 
    //Service里面Dao用了依赖注入,这便不能用new对象的方式
    @Autowired
    IWorkFlowService workFlowServiceImpl;
 
    @Test
    public void selectWorkInstById() {
        TmWorkInst workInst = new TmWorkInst();
        List<TmWorkInst> ss = workFlowServiceImpl.selectWorkInstById(workInst);
        //添加断言(判空) 
        assertNull(ss);
        for (TmWorkInst o : ss)
            System.out.println("===============" + o.toString());
    }
}

查看日志:
在这里插入图片描述

更多推荐