为什么我身边的同事不喜欢写单元测试?

发表于:2020-4-27 09:29

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

 作者:小鱼吃猫    来源:小鱼与Java

  有些人认为,写单元测试就是在浪费时间 ,写完代码,依然还是能够进行测试的。但是,还是建议写单元测试的,可以让你的条理更加清晰,而且当某个功能出现问题时,可能通过单元测试很容易的定位和解决问题。本文主要总结下在Spring及SpringBoot项目中,使用单元测试时的方法。将JUnit4和JUnit5对比着来写,因为我发现我身边的同事经常搞不明白要怎么用。
  1.Juint版本说明
  这里主要说明下它们在Maven下的依赖包
  依赖  JUint4
   <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.13</version>
  <!--请注意这个scope的用法-->
  <scope>test</scope>
  </dependency>
  依赖  JUint5
   <dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>5.6.2</version>
  <!--请注意这个scope的用法-->
  <scope>test</scope>
  </dependency>
  在上边的依赖中,两个依赖分别写了scope属性,这里做一个讲解:
  众所周知,一个标准的maven项目结构如下图:
  写Java代码的地方有两个src/main/java和src/test/java。如果我们不在上边依赖中添加scope为test属性,就可以在这两个地方任意地方写@Test测试方法,但是,如果添加了这个属性,就只能在src/test/java下写单元测试代码,这个就是maven所谓的test域。从上图也可以看出,test域可以有自己的配置文件,如果没有的话就会去加载main下的resources的配置文件,如果有,则以自己的为优先。
  2.JUnit5常见注解及其用法
  不管使用哪个版本,一个标准的单元测试方法如下所示:
   public class TestDemo {
  @Test
  void fun1(){
  System.out.println("欢迎关注微信公众号——51Testing软件测试网");
  }
  }
  但是对于Junit4而言,所有的测试方法应当是public声明的,而Junit5不用。只不过不同的版本,这个@Test的类是不同的:
   Junit4: org.junit.Test
  Junit5: org.junit.jupiter.api.Test
  相比Junit4而言,JUnit5添加了新的一些注解,但是常用的注解还是相同的,主要有以下:
  以上是在JUnit5中最常用的注解,可以自己挨个试下,一下子就会明白其用法。关注我,后续为您递上具体用法。
  3.普通Maven项目中使用Junit
  引入相关依赖后,然后在对应的位置进行测试就可以了,这里不做演示,可以自行下载代码查看
  4.Spring项目中使用Junit
  这里的Spring和SpringBoot项目也是基于Maven构建的,和普通Maven项目的最大区别就是加载Sprign容器而已,一般来说,使用Spring提供的上下文ApplicationContext就可以从配置文件件或者配置类加载Spring容器。如下代码:
   /**
  * 使用普通的Spring上下文来加载Spring容器
  *
  * @auther 微信公众号:51Testing软件测试网
  * 2020/4/23
  */
  public class MyMain {
  public static void main(String[] args) {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
  Teacher teacher = (Teacher) ctx.getBean("teacher");
  System.out.println(teacher.getName());
  }
  }
  但是,我们可以通过引入Spring相关的test依赖来让其自动加载Spring上下文,这样我们就能利用如@Autowired这样的自动注入方式来获取bean了
   <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>5.2.5.RELEASE</version>
  </dependency>
  但是这里对于JUnit4和JUnit5写测试方法时有一点儿不同之处,如下:
  1.Junit4
   @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"classpath:application.xml"})
  public class TestDemo {
  @Resource
  private Teacher teacher;
  @Test
  public void fun(){
  System.out.println(teacher.getName());
  }
  }
  2.Junit5
   @SpringJUnitConfig
  //指定配置文件路径,会先从test域中找
  @ContextConfiguration("classpath:application.xml")
  public class SpringTest {
  @Resource
  private Teacher teacher;
  @Test
  void fun(){
  System.out.println(teacher.getName());
  }
  }
  仔细看上边这两段代码中的注解,虽然用的注解不同,但是它们是用来加载Spring上下文的
  5.SpringBoot项目中使用Junit
  在SpringBoot中,为我们提供了一个SpringBootTest的注解来加载Spring容器。在SpringBoot2.2.0以前是JUnit4,在SpringBoot之后是JUnit5。但是我建议最应该使用JUnit5。
  1.Junit4
  pom.xml中的依赖
   <dependencies>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.1.6.RELEASE</version>
  </dependency>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <version>2.1.6.RELEASE</version>
  <!--表示只能在maven的测试域中使用-->
  <scope>test</scope>
  </dependency>
  </dependencies>
  测试类
   @SpringBootTest
  @RunWith(SpringJUnit4ClassRunner.class)
  public class TestDemo {
  @Resource
  private Student student;
  @Test
  public void fun1(){
  System.out.println(student.getName());
  }
  }
  2.Junit5
  pom.xml中的依赖
   <dependencies>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.2.6.RELEASE</version>
  </dependency>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <version>2.2.6.RELEASE</version>
  <!--表示只能在maven的测试域中使用-->
  <scope>test</scope>
  <exclusions>
  <!--这个是JUnit5中为了支持使用JUint4所做的一个过度
  也就是说,你只需要在你的JUnit4旧项目中添加这个依赖,
  就能完美过渡,而不用修改之前代码
  这里用不到,自然也就排除了。当然,这里,它无关紧要
  -->
  <exclusion>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  </exclusion>
  </exclusions>
  </dependency>
  </dependencies>
  测试类的代码
   @SpringBootTest //它默认会为我们加载Spring容器,
  public class TestDemo {
  @Resource
  private Student student;
  @Test
  void fun1(){
  System.out.println(student.getName());
  }
  }
  为什么在SpringBoot中不用指定Spring容器的配置文件?
  其实他是会自动加载类路径下的那个SpringBoot的启动类的,就算指定配置文件,也是指定那个启动类为配置类。如果你写的包结构不符合它的要求,就需要自己使用@ContextConfiguration注解来指定Spring的配置类了

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

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号