SpringBoot中使用JNnit4(入门篇)
一、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
二、在web项目(即含有SpringApplication启动类)中test目录新建测试类,包路径和SpringApplication中的扫描路径一致,否则无法启动测试类。
若测试类的包路径和启动类的包路径不一致,会出现以下错误信息:
Neither @ContextConfiguration nor @ContextHierarchy found for test class [xx.xx.Test], using SpringBootContextLoader
Could not detect default resource locations for test class [xx.xx.Test]: no resource found for suffixes {-context.xml, Context.groovy}.
Could not detect default configuration classes for test class [xx.xx.Test]: Test does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
三、测试类添加
package xx.xx.test; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTests { @Before
public void init(){
System.out.println("******测试开始");
} @After
public void end(){
System.out.println("******测试结束");
} @BeforeClass
public static void initClass(){
System.out.println("******测试开始初始化");
} @AfterClass
public static void endClass(){
System.out.println("******测试结束初始化");
}
}
package xx.xx.test; public class Test extends ApplicationTests{ @Autowired
private BtsTradeGoodsService btsTradeGoodsService; @org.junit.Test
public void add(){
btsTradeGoodsService.add();
} }
然后在有@Test注解方法的类中使用JUint启动。
在@Test注解的方法中,和平时开发项目调用接口是一样的。
四、注解
1、类注解
@RunWith:
1.表示运行方式,@RunWith(JUnit4TestRunner)、@RunWith(SpringRunner.class)、@RunWith(PowerMockRunner.class) 三种运行方式,分别在不同的场景中使用。
1.当一个类用@RunWith注释或继承一个用@RunWith注释的类时,JUnit将调用它所引用的类来运行该类中的测试而不是开发者去在junit内部去构建它。我们在开发过程中使用这个特性。
@SpringBootTest:
1.注解制定了一个测试类运行了Spring Boot环境。提供了以下一些特性:
1.1.当没有特定的ContextConfiguration#loader()(@ContextConfiguration(loader=...))被定义那么就是SpringBootContextLoader作为默认的ContextLoader。
1.2.自动搜索到SpringBootConfiguration注解的文件。
1.3.允许自动注入Environment类读取配置文件。
1.4.提供一个webEnvironment环境,可以完整的允许一个web环境使用随机的端口或者自定义的端口。
1.5.注册了TestRestTemplate类可以去做接口调用。
2.添加这个就能取到spring中的容器的实例,如果配置了@Autowired那么就自动将对象注入。
@WebAppConfiguration:
由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebIntegrationTest("server.port:0"):
使用0表示端口号随机,也可以具体指定如8888这样的固定端口。不可和@WebAppConfiguration同时使用。
2、方法注解
@BeforeClass:方法只能是static void。
@AfterClass:方法只能是static void。
@Before:@Test运行之前调用的方法,可以做初始化操作
@After:执行完测试用例需要执行的清理工作
@Test:测试用例的单元
@Mock:
有点类似Autowired注解,而@Mock注解是自动实现模拟对象,而并非Bean实例创建。
正式环境只是一个接口,并没有实现,也并没有纳入spring容器进行管理。使用BDDMockito对行为进行预测。
@Ignore("not ready yet"):该方法不执行
执行顺序是:@BeforeClass→@Before→@Test→@After→@AfterClass
当启动测试类,测试类中有多个@Test,@BeforeClass和@AfterClass只会执行一次,每一个@Test都会执行一次@Before和@After。
五、对Controller进行测试
1、使用浏览器直接访问:
http://localhost:8080/index
http://localhost:8080/show?id=100
2、使用测试类:
package xx.xx.ctrl; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserCtrl { @GetMapping("/index")
public String index(){
System.out.println("UserCtrl.index");
return "UserCtrl.index";
} @GetMapping("/show")
public String show(@RequestParam("id")String id){
System.out.println("UserCtrl.show:" + id);
return "UserCtrl.show:" + id;
}
}
package xx.xx.test; 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; @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTests { @Before
public void init(){
System.out.println("******测试开始");
} @After
public void end(){
System.out.println("******测试结束");
} @BeforeClass
public static void initClass(){
System.out.println("******测试开始初始化");
} @AfterClass
public static void endClass(){
System.out.println("******测试结束初始化");
}
}
package xx.xx.test; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate; public class Test extends ApplicationTests{ @Autowired
private TestRestTemplate testRestTemplate; @org.junit.Test
public void toIndex(){
String t = testRestTemplate.getForObject("/index", String.class);
System.out.println("toIndex:" + t);
} @org.junit.Test
public void toShow(){
String t = testRestTemplate.getForObject("/show?id=99", String.class);
System.out.println("toShow:" + t);
} @Override
public void init(){
System.out.println("******重写测试开始");
}
}
使用这种方式,在@SpringBootTest注解中一定要添加webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
最新文章
- 循序渐进做项目系列(4)迷你QQ篇(2)——视频聊天!(附源码)
- jquery懒加载jquery.lazyload.js
- IntegerCache类
- MVC认知路【点点滴滴支离破碎】【二】----Razor服务器标记语言
- swift 闭包
- Google FlatBuffers——开源、跨平台的新一代序列化工具
- jQuery使用之(二)设置元素的样式
- 配置VS2010具有代码提示功能
- Drupal常用开发工具(一)——Devel模块
- Spring入门(5)-自动装配Bean属性
- PT100运算放大器电路
- WCF 绑定(Binding)
- mysql.cnf 配制文件详解
- ubuntu 14.04 opencv2.4.13 安装
- Linux文件
- 【NOIP2015】斗地主(搜索,贪心)
- C++ 初读迭代器
- (链表) lintcode 219. Insert Node in Sorted Linked List
- ahjesus Axure RP 8.0注册码,亲测可用
- 更改MyEclipse中的src目录的浏览方式