SpringBoot:SpringBoot项⽬进⾏单元测试
SpringBoot:SpringBoot项⽬进⾏单元测试
JUnit是⼀个回归测试框架,被开发者⽤于实施对应⽤程序的单元测试,加快程序编制速度,同时提⾼编码的质量。
JUnit中常⽤的的注解如下:
@BeforeClass:针对所有测试,只执⾏⼀次,且必须为static void。
@Before:初始化⽅法,执⾏当前测试类的每个测试⽅法前执⾏。
@Test:测试⽅法,在这⾥可以测试期望异常和超时时间。
@Test(timeout = 1000) 测试⽅法执⾏超过1000毫秒后算超时,测试将失败。
@Test(expected = Exception.class) 测试⽅法期望得到的异常类,如果⽅法执⾏没有抛出指定的异常,则测试失败。
@After:释放资源,执⾏当前测试类的每个测试⽅法后执⾏。
@AfterClass:针对所有测试,只执⾏⼀次,且必须为static void。
@Ignore:忽略的测试⽅法(只在测试类的时候⽣效,单独执⾏该测试⽅法⽆效)。
@RunWith:可以更改测试运⾏器 ,缺省值 org.junit.runner.Runner
⼀个单元测试类执⾏顺序为:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每⼀个测试⽅法的调⽤顺序为:
@Before –> @Test –> @After
Spring Boot进⾏单元测试,主要分为不依赖web模块的单元测试(Service)和依赖web模块的单元测试(Controller)两种。测试步骤如下:
1、在pom中添加Jar包依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、不依赖web模块的单元测试(Service)
ample.bean.Student;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.context.SpringBootTest;
import st.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class StudentServiceTest {
@Autowired
private StudentService studentService;
@Test
public void findOne() throws Exception {
//测试Id为1的学⽣年龄是否为20
Student student = studentService.findOne(1);
Assert.assertEquals(new Integer(20), Age());
}
}
3、依赖web模块的单元测试(Controller)
get请求访问Testcontroller,路径+"/hello"返回hello字符串。验证接⼝是否正常以及返回预期结果判定如下:
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class SpringbootDemoApplicationTests {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Test
public  void hello() throws Exception {
String url = "/hello";//访问url
String expectedResult = "hello";//预期返回结果
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
MvcResult mvcResult = mockMvc.(url).accept(MediaType.APPLICATION_JSON)).andReturn();
//访问返回状态
int status = Response().getStatus();
System.out.println(status);
//接⼝返回结果
String content = Response().getContentAsString();
System.out.println(content);
//打印结果和状态
//断⾔状态
Assert.assertTrue("成功", status == 200);
Assert.assertFalse("失败", status != 200);
//断⾔结果
Assert.assertTrue("数据⼀致", expectedResult.equals(content));
Assert.assertFalse("数据不⼀致", !expectedResult.equals(content));
}
}
解释:
A. @SpringBootTest注解是普通的 Spring 项⽬(⾮ Spring Boot 项⽬)中编写集成测试代码所使⽤的@ContextConfiguration注解的替代品。其作⽤是⽤于确定如何装载 Spring 应⽤程序的上下⽂资源。
springboot框架的作用
当运⾏ Spring Boot 应⽤程序测试时,@SpringBootTest注解会⾃动的从当前测试类所在的包起⼀层⼀层向上搜索,直到到⼀个
@SpringBootApplication或@SpringBootConfiguration注释类为⽌。以此来确定如何装载 Spring 应⽤程序的上下⽂资源。
B. 基于 Spring 环境的 Junit 集成测试还需要使@RunWith(SpringJUnit4ClassRunner.class)注解,该注解能够改变 Junit 并让其运⾏在 Spring 的测试环境,以得到 Spring 测试环境的上下⽂⽀持。否则,在 Junit 测试中,Bean 的⾃动装配等注解将不起作⽤。但由于SpringJUnit4ClassRunner 不⽅便记忆,Spring 4.3 起提供了⼀个等同于 SpringJUnit4ClassRunner 的类 SpringRunner,因此可以简写成:@RunWith(SpringRunner.class)。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。