SpringBoot2整合Junit4和Junit5(junit springboot)

  本篇文章为你整理了SpringBoot2整合Junit4和Junit5(junit springboot)的详细内容,包含有junit5 springboot junit springboot springboot整合junit需要的依赖 springboot2 junit SpringBoot2整合Junit4和Junit5,希望能帮助你了解 SpringBoot2整合Junit4和Junit5。

  (一)SpringBoot版本、IDEA、Junit的版本对应

  

https://blog.csdn.net/weixin_44457062/article/details/122950232#:~:text=Spring%20Boot%20%E5%9C%A8,2.4.0%20%E7%89%88%E6%9C%AC%E4%B9%8B%E5%90%8E%EF%BC%8C%E4%BE%BF%E4%B8%8D%E5%86%8D%E5%85%BC%E5%AE%B9Junit4%E3%80%82%20%E6%89%80%E4%BB%A5%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%AD%A4%E6%97%B6%E4%BD%BF%E7%94%A8Idea%E7%89%88%E6%9C%AC%E5%9C%A82017.3.5%E4%B9%8B%E5%89%8D%E7%89%88%E6%9C%AC%EF%BC%8C%E9%82%A3%E4%B9%88%E7%A8%8B%E5%BA%8F%E4%B8%AD%E5%8F%AA%E8%83%BD%E5%BC%95%E7%94%A8Junit5%E3%80%82

 

  

 

  (二)官方文档

  

SpringBoot所有版本官方文档:https://docs.spring.io/spring-boot/docs/

 

  

 

  (三)演示环境

  

Maven 3.6.3

 

  Spring Boot 2.3.4.RELEASE(直接继承spring-boot-starter-parent:2.3.4.RELEASE)

  JUnit 4.13.1(自己选的比较新的Junit4依赖)

  Junit 5.6.2(spring-boot-starter-test:2.3.4.RELEASE默认的)

  

 

  二、整合JUnit5

  (一)spring-boot-starter-test:2.3.4.RELEASE默认的依赖

  

The spring-boot-starter-test “Starter” (in the test scope) contains the following provided libraries:

 

  1、JUnit 5 (including the vintage engine for backward compatibility with JUnit 4): The de-facto standard for unit testing Java applications.

  2、Spring Test Spring Boot Test: Utilities and integration test support for Spring Boot applications.

  3、AssertJ: A fluent assertion library.

  4、Hamcrest: A library of matcher objects (also known as constraints or predicates).

  5、Mockito: A Java mocking framework.

  6、JSONassert: An assertion library for JSON.

  7、JsonPath: XPath for JSON.

  

 

  (二)引入依赖

  

The starter also brings the vintage engine so that you can run both JUnit 4 and JUnit 5 tests. If you have migrated your tests to JUnit 5, you should exclude JUnit 4 support, as shown in the following example:(大概意思就是如果只使用JUnit5就要排除掉排除JUnit4支持vintage engine,不排除就是兼容JUnit4和JUnit5的。

 

   dependency

   groupId org.springframework.boot /groupId

   artifactId spring-boot-starter-test /artifactId

   scope test /scope

   exclusions

   exclusion

   groupId org.junit.vintage /groupId

   artifactId junit-vintage-engine /artifactId

   /exclusion

   /exclusions

   /dependency

  

 

  (三)测试类代码

  

package com.online.store.dao;

 

  import com.online.store.product.ProductApplication;

  import com.online.store.product.dao.CategoryDao;

  import com.online.store.product.entity.CategoryEntity;

  import org.junit.jupiter.api.Test;

  import org.springframework.boot.test.context.SpringBootTest;

  import javax.annotation.Resource;

  import java.util.List;

  //ProductApplication是我的主启动类,换成自己的就可以

  @SpringBootTest(classes = ProductApplication.class)

  public class CategoryDaoTest {

   @Resource

   private CategoryDao categoryDao;

   //注意:整合JUnit5的@Test注解为org.junit.jupiter.api.Test里的

   @Test

   public void selectList() {

   List CategoryEntity categoryEntityList=categoryDao.selectList(null);

  

 

  三、整合JUnit4

  (一)spring-boot-starter-test:2.3.4.RELEASE默认兼容Junit4的依赖

  

org.springframework:spring-test:5.2.9.RELEASE

 

  org.springframework.boot:spring-boot-test:2.3.4.RELEASE

  #这个就是上面排除的vintage engine依赖

  org.junit.vintage:junit-vintage-engine:5.6.2

  

 

  (二)引入依赖

  

 dependency 

 

   groupId org.springframework.boot /groupId

   artifactId spring-boot-starter-test /artifactId

   scope test /scope

   /dependency

   dependency

   groupId junit /groupId

   artifactId junit /artifactId

   version 4.13.1 /version

   scope test /scope

   /dependency

  

 

  (三)测试类代码

  

package com.online.store.dao;

 

  import com.online.store.product.ProductApplication;

  import com.online.store.product.dao.CategoryDao;

  import com.online.store.product.entity.CategoryEntity;

  import org.junit.Test;

  import org.junit.runner.RunWith;

  import org.springframework.boot.test.context.SpringBootTest;

  import org.springframework.test.context.junit4.SpringRunner;

  import javax.annotation.Resource;

  import java.util.List;

  //整合JUnit4需要加上@RunWith(SpringRunner.class)

  @RunWith(SpringRunner.class)

  //ProductApplication是我的主启动类,换成自己的就可以

  @SpringBootTest(classes = ProductApplication.class)

  public class CategoryDaoTest {

   @Resource

   private CategoryDao categoryDao;

   //注意:整合JUnit4的@Test注解为org.junit.Test里的

   @Test

   public void selectList() {

   List CategoryEntity categoryEntityList=categoryDao.selectList(null);

  

 

  以上就是SpringBoot2整合Junit4和Junit5(junit springboot)的详细内容,想要了解更多 SpringBoot2整合Junit4和Junit5的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: