SpringBoot 使用 Junit
来源:互联网 发布:淘宝自粘墙纸有甲醛吗 编辑:程序博客网 时间:2024/06/16 21:35
一、Spring Boot使用Junit步骤
1). 加入Maven的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2). 编写测试service:
新建com.spencer.service.HelloService 提供测试方法:
package com.spencer.service;
import org.springframework.stereotype.Service;
@Service
publicclass HelloService {
public String sayHello(){
return "hello!";
}
}
3). 编写测试类:
在src/test/Java下编写测试类:com.spencer.test.HelloServiceTest:
package com.spencer.test;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.boot.test.context.SpringBootTest;
import com.spencer.App;
import com.spencer.service.HelloService;
//// SpringJUnit支持,由此引入Spring-Test框架支持!
@RunWith(SpringJUnit4ClassRunner.class)
//// 指定我们SpringBoot工程的Application启动类
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = App.class)
public class HelloServiceTest {
@Resource
private HelloService helloService;
@Test
publicvoid testGetName(){
Assert.assertEquals("hello",helloService.getName());
}
}
这时候就可以进行测试了,右键—Run As – Junit Test。
二、Junit基本注解介绍
//在所有测试方法前执行一次,一般在其中写上整体初始化的代码
@BeforeClass
//在所有测试方法后执行一次,一般在其中写上销毁和释放资源的代码
@AfterClass
//在每个测试方法前执行,一般用来初始化方法(比如我们在测试别的方法时,类中与其他测试方法共享的值已经被改变,为了保证测试结果的有效性,我们会在@Before注解的方法中重置数据)
@Before
//在每个测试方法后执行,在方法执行完成后要做的事情
@After
// 测试方法执行超过1000毫秒后算超时,测试将失败
@Test(timeout = 1000)
// 测试方法期望得到的异常类,如果方法执行没有抛出指定的异常,则测试失败
@Test(expected = Exception.class)
// 执行测试时将忽略掉此方法,如果用于修饰类,则忽略整个类
@Ignore(“not ready yet”)
@Test
@RunWith
在JUnit中有很多个Runner,他们负责调用你的测试代码,每一个Runner都有各自的特殊功能,你要根据需要选择不同的Runner来运行你的测试代码。
如果我们只是简单的做普通Java测试,不涉及Spring Web项目,你可以省略@RunWith注解,这样系统会自动使用默认Runner来运行你的代码。
- SpringBoot 使用 Junit
- SpringBoot 使用JUnit 单元测试
- SpringBoot-JUnit
- springBoot junit Test
- SpringBoot Junit单元测试
- SpringBoot+Junit Test
- SpringBoot junit单元测试
- SpringBoot的Junit单元测试
- SpringBoot中的Junit测试
- SpringBoot junit测试
- 二、SpringBoot 配置 Junit Json
- Junit和Spring、SpringBoot测试
- springboot junit 每天进步百分之一
- springboot第二天 junit单元测试
- SpringBoot开发Junit单元测试方法
- SpringBoot与JUnit+Mockito 单元测试
- junit 使用
- junit使用
- (HDU
- Unity LayerMask的位运算
- SEO技术问答:网站被黑被劫持了怎么办
- Tensorflow学习: Slim tutorial
- HAWQ + MADlib 玩转数据挖掘之(十)——图算法之单源最短路径
- SpringBoot 使用 Junit
- sqoop入门命令
- struts.xml配置及Action操作
- 针对Intellij IDEA 2017闪退的解决方案
- 微信分享没有缩略图
- jsonp使用
- file_get_contents,fopen,curl 获取远程页面
- 广播监听网络状态
- Choose and Divide,Uva10375(数论之分解质因数)