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)

import com.example.bean.Student; 

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.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), student.getAge());
    }
}

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.perform(MockMvcRequestBuilders.get(url).accept(MediaType.APPLICATION_JSON)).andReturn();      
      
      //访问返回状态
      int status = mvcResult.getResponse().getStatus();
      System.out.println(status);
      
      //接口返回结果
      String content = mvcResult.getResponse().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 应用程序的上下文资源。
     当运行 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)。
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王小二(海阔天空)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值