Spring项目单元测试,测试service层、controller层(对spring restfull 接口单元测试)
下载地址:https://download.****.net/download/zhaoqingkaitt/10767529
项目介绍:
通过java配置的方式搭建的一个基本的web项目,零xml配置,主要用于单元测试示例,使用了spring支持h2内存数据库,不需要额外安装数据库,可以测试controller层、service层,项目使用的打包工具为gradle,比maven配置更方便、简洁
项目结构:
-
1、Junit基本示例
-
1.1、断言
断言是测试的心脏,用于判断预期的内容是否与实际一致。Junit框架提供了org.junit.Assert类,这里面封装了一套静态方法,用于验证期望值与实际值逻辑比对是否正确。assertTrue(boolean condition) 用于判断一个条件为真,如果为false会抛出异常,assertFalse(boolean condition) 用于判断一个条件为假,如果为真会抛出异常assertEquals(..........) 用于判断前后相等
package junit.base;
import org.junit.Test;
import static org.junit.Assert. * ;
/**
* Created by zhaott on 2018/9/18.
*/
public class JunitDemo02 {
@Test
public void demo02(){
Object user=this.getUserById();
assertNotNull(user);
}
/**
* 模拟service中的方法
* @return
*/
private Object getUserById(){
return null;
}
}
-
1.2 before、after
用于在测试前、后做一些初始化、收尾工作,例如测试接口性能。
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Created by zhaott on 2018/9/18.
*/
public class JunitDemo03 {
//开始时间
long startTimeMillis;
//结束时间
long endTimeMillis;
@Before
public void before(){
startTimeMillis=System.currentTimeMillis();
}
@After
public void after(){
endTimeMillis= System.currentTimeMillis();
System.out.println("【耗时】"+(endTimeMillis-startTimeMillis));
}
@Test
public void demo03(){
this.query();
}
private void query(){
try {
Thread.sleep((long) (Math.random()*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
-
1.3 通过timeout设置超时间
有时可能需要发送一些网络相关的请求,或着一些其它可能比较费时的操作,但我们并不想等待太长时间,这是就可以用timeout设置最大的一个等待时间。通过这个,也可以对一批接口进行测试,可以方便的知道,哪些接口的性能不符合要求。
import org.junit.Test;
/**
* Created by zhaott on 2018/9/18.
*/
public class JunitDemo04 {
@Test(timeout = 3000)
public void demo04(){
this.httpRequest();
}
private void httpRequest(){
try {
Thread.sleep((long) (Math.random() * 7000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
-
1.4 通过expected设置符合预期的异常
如何验证代码是否按预期抛出异常?验证代码是否正常完成非常重要,但确保代码在特殊情况下的行为符合预期也至关重要。
import org.junit.Test;
/**
* Created by zhaott on 2018/9/18.
*/
public class JunitDemo05 {
@Test(expected = NullPointerException.class)
public void demo05(){
String str=null;
System.out.println(str.length());;
}
}
-
1.5 通过Parameterized进行参数测试
当我们写了一个复杂的算法,可能我们随便测试了几个值,返回的结果正好是我们期待的结果,但我们把项目部署后,偶尔会出现不正常的现象,原因发现某些边缘值没有被测试到,所以要进行大量参数的测试。如何将一批参数与期待值传给测试用例,就可以用Parameterized来达到我们的目的。
-
1.6 通过suite进行批量测试
通过suite可以将多个测试类,放到一块同时执行。
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Created by zhaott on 2018/9/18.
*/
@RunWith(Suite. class )
@Suite.SuiteClasses( {
JunitDemo01. class ,
JunitDemo02. class
} )
public class JunitDemo07 {
}
-
2 、service层测试示例
import junitdemo.config.JunitDemoRootConfig;
import junitdemo.service.JunitDemoService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
/**
* Created by zhaott on 2018/10/30.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JunitDemoRootConfig.class})
@WebAppConfiguration
public class JunitDemoServiceTest {
@Autowired
private JunitDemoService service;
@Test
public void get(){
System.out.println(service.get("id"));
}
}
-
3、controller层测试示例
对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便,依赖网络环境等,所以为了可以对Controller进行测试,我们引入了MockMVC。 MockMvc实现了对Http请求的模拟,能够直接使用网络的形式,转换到Controller的调用,这样可以使得测试速度快、不依赖网络环境,而且提供了一套验证的工具,这样可以使得请求的验证统一而且很方便。
-
3.1、get请求获取详情
@Test
public void details() {
try {
String rsp = mockMvc
.perform(MockMvcRequestBuilders.get(uri+"/123"))
.andReturn()
.getResponse()
.getContentAsString();
System.out.println("rest请求结果如下:\n"+rsp);
} catch (Exception e) {
e.printStackTrace();
}
}
-
3.2、delete请求,删除数据
@Test
public void delete() {
try {
String rsp = mockMvc
.perform(MockMvcRequestBuilders.delete(uri+"/123"))
.andReturn()
.getResponse()
.getContentAsString();
System.out.println("rest请求结果如下:\n"+rsp);
} catch (Exception e) {
e.printStackTrace();
}
}