断言
断言(assertions)是测试方法中的核心部分,用来对测试需要满足的条件进行验证。这些断言方法都是 org.junit.jupiter.api.Assertions 的静态方法。JUnit 5 内置的断言可以分成如下几个类别:
1、简单断言
2、数组断言
通过 assertArrayEquals 方法来判断两个对象或原始类型的数组是否相等
配置类中
也可以设置异常报文,第一个参数是期望值,第二个参数是实际值
@Test@DisplayName("数组断言测试")voidtestArrayAssertions(){Assertions.assertArrayEquals(newint[]{1,2},newint[]{1,2},"元素不一样");}
详细步骤:
Spring boot目录结构一般是固定的:
选中要测试的方法体右键:
会弹出新建测试类的框
上面一个是我测试建的,如果要新建应该选第二个:Create New Test…
上面是生成测试类的名称以及测试的一些属性,下面是选择你要放入的待测试的模块:
下面开始上代码:
Spring boot自带的测试基类(启动配置):
packagecom.ai.rai.group.workflow.application;importorg.junit.runner.RunWith;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.boot.test.context.SpringBootTest;importorg.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)publicclassBaseTestApplication{protectedLogger logger =LoggerFactory.getLogger(this.getClass());}
测试类启动入口(一般都有的,这边也贴一下吧):
packagecom.ai.rai.group.workflow.application;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.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"})publicclassTestApplication{publicstaticvoidmain(String[] args){SpringApplication.run(TestApplication.class,args);}}
待测试的Service方法就不贴了,根据自己的实际情况写逻辑就行;
测试类(使用依赖注入):
packagecom.ai.rai.group.workflow.service.impl;importcom.ai.rai.group.workflow.application.BaseTestApplication;importcom.ai.rai.group.workflow.entity.beans.TmWorkInst;importcom.ai.rai.group.workflow.service.IWorkFlowService;importorg.apache.commons.collections.bag.SynchronizedSortedBag;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importsun.tools.jar.Main;importjava.util.List;importstaticorg.junit.Assert.*;@RunWith(SpringJUnit4ClassRunner.class)publicclassWorkFlowServiceImpllswTestextendsBaseTestApplication{//Service里面Dao用了依赖注入,这便不能用new对象的方式@AutowiredIWorkFlowService workFlowServiceImpl;@TestpublicvoidselectWorkInstById(){//这个用的之前写好的Service测试,TmWorkInst workInst =newTmWorkInst();List<TmWorkInst> ss = workFlowServiceImpl.selectWorkInstById(workInst);for(TmWorkInst o : ss)System.out.println("==============="+o.toString());}}
控制台部分信息打印:
加断言
正常的单元测试流程还需要加断言打印日志
加断言可以避免对数据库的误操作,以及缩短测试流程(断言失败就不再执行之后的代码了)
代码:
packagecom.ai.rai.group.workflow.service.impl;importcom.ai.rai.group.workflow.application.BaseTestApplication;importcom.ai.rai.group.workflow.entity.beans.TmWorkInst;importcom.ai.rai.group.workflow.service.IWorkFlowService;importorg.apache.commons.collections.bag.SynchronizedSortedBag;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importsun.tools.jar.Main;importjava.util.List;importstaticorg.junit.Assert.*;@RunWith(SpringJUnit4ClassRunner.class)publicclassWorkFlowServiceImpllswTestextendsBaseTestApplication{//Service里面Dao用了依赖注入,这便不能用new对象的方式@AutowiredIWorkFlowService workFlowServiceImpl;@TestpublicvoidselectWorkInstById(){TmWorkInst workInst =newTmWorkInst();List<TmWorkInst> ss = workFlowServiceImpl.selectWorkInstById(workInst);//添加断言(判空) assertNull(ss);for(TmWorkInst o : ss)System.out.println("==============="+ o.toString());}}
查看日志:
版权归原作者 qq_16570607 所有, 如有侵权,请联系我们删除。