SpringBoot整合junit测试案例

发表于:2022-5-31 09:49

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:热河不是河    来源:51CTO博客

#
JUnit
分享:
  1.之前开发项目是不要求写单测的,最近公司管理严格需要对开发的功能编写单测,所以在此记录下springboot对junit的整合以及使用的方式。
  2.引入需要用到的依赖jar包,一般创建好springboot项目都会自带test依赖。
  3.一般我们新建的springboot项目都带有测试包,我们直接使用,在里面编写测试类即可。
  4.因为项目中可能会存在很多测试类,那么就会存在很多注解重复被添加的冗余,因此我们写一个基类,其他测试类只需要继承基类就行,基类名字就叫做BaseTestClass,如下:

  package com.example.mybatisplus;
  import org.junit.After;
  import org.junit.Before;
  import org.junit.runner.RunWith;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.test.context.web.WebAppConfiguration;
  @SpringBootTest
  @WebAppConfiguration
  @RunWith(SpringRunner.class)
  public class BaseTestClass {
      @Before
      public void init(){
          System.out.println("before");
      }
      @After
      public void after(){
          System.out.println("after");
      }
  }

  5.然后就可以编写测试类了,测试类里面注入service层,然后通过断言进行返回数据的判断,下面就是我测试的一个查询接口的测试类:

  package com.example.mybatisplus;
  import com.example.mybatisplus.entity.Employee;
  import com.example.mybatisplus.service.EmployeeService;
  import org.junit.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import static org.junit.Assert.assertEquals;
  public class OperateDataTest extends BaseTestClass{
      @Autowired
      private EmployeeService employeeService;
      @Test
      public void testQueryById(){
          Employee employee = employeeService.selectById("2");
          assertEquals("Jerry",employee.getLastName());
          assertEquals("jerry@qq.com",employee.getEmail());
      }
  }

  注意:由于业务逻辑层都是要求写在service层,所以我们这里就注入了service进行测试。
  那有同学要问了,我直接测试controller层可以吗?答案肯定是可以的,下面我就演示一下直接注入controller层,其实和service是一样的,如下所示:

  package com.example.mybatisplus;
  import com.example.mybatisplus.controller.MybatisPlusController;
  import com.example.mybatisplus.entity.Employee;
  import com.example.mybatisplus.service.EmployeeService;
  import org.junit.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import java.util.List;
  import static org.junit.Assert.assertEquals;
  public class OperateDataTest extends BaseTestClass{
      @Autowired
      private EmployeeService employeeService;
      @Autowired
      private MybatisPlusController mybatisPlusController;
      @Test
      public void testQueryById(){
          Employee employee = employeeService.selectById("2");
          assertEquals("Jerry",employee.getLastName());
          assertEquals("jerry@qq.com",employee.getEmail());
      }
      @Test
      public void testController(){
          List<Employee> employeeList = mybatisPlusController.queryList();
          System.out.println("employee="+employeeList.get(0));
      }
  }

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号