springboot使用junit5/junit4

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
其中
JUnit平台,其主要作用是在JVM上启动测试框架。它定义了一个抽象的TestEngineAPI来定义运行在平台上的测试框架,同时还支持通过命令行、Gradle和Maven来运行平台。
JUnit Jupiter,包含了JUnit5最新的编程模型和扩展机制。
JUnit Vintage,允许在平台上运行JUnit3和JUnit4的测试用例。
JUnit5对Java运行环境的最低要求是Java8,同时也兼容测试旧版本JDK编译出来的代码。

完整依赖:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.5.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.5.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.5.2</version>
    <scope>test</scope>
</dependency>

2018年10月24日Maven 3.6.0发布,Maven才正式原生支持Junit5。在这个版本中,Maven团队一并发布了 Maven Surefire Plugin 2.22.0 和Maven Failsafe plugin 2.22.0,进而解决了对Junit5的支持问题。
在此之前,为了能在Maven中运行Junit5的测试用例,需要为 Maven Surefire plugin额外提供一个Junit5团队提供的Junit Provider。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>
</plugin>

如果将Maven升级到3.6.0及以上版本,那么junit-platform-surefire-provider这个依赖就不需要了。

// UserService类
@Service
public class UserServiceImpl implements UserService {

    @Override
    public void printName() {
        System.out.println("UserServiceImpl");
    }
}

1.springboot2.2.0之前,spring-boot-starter-test默认支持的是junit4;
我这里的测试环境:spring boot2.1.0+maven3.5.4+jdk8+idea2019.1.3

   1.1junit4的测试:
	需要的的依赖:
	 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
     </dependency>
	
	测试类举例:
	@RunWith(SpringRunner.class)
	@SpringBootTest  // 如果启动报错,则需要指定启动类的class
	public class Springboot159ApplicationTests {
	    
		@Autowired
		UserService userService;
		@Test
		public void contextLoads() throws SQLException {
			userService.printName();
			
		}

	}
	 
    1.2junit5的测试:
	需要的的依赖:
	 <dependencies>
	 <!-- exclude junit 4 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- junit 5 跟随spring-boot-dependencies版本 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
		
		<!-- 自已指定 这个版本还没有加入依赖管理 -->
		 <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.3.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>

        </plugins>
    </build>
	
	测试类举例:
    import com.ysy.HelloDemo;
	import com.ysy.service.UserService;
	import org.junit.jupiter.api.DisplayName;
	import org.junit.jupiter.api.Test;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.boot.test.context.SpringBootTest;

	/**
	 * Created by Administrator on 2020/1/31 14:07
	 */
	@SpringBootTest(classes= HelloDemo.class)
	//@ExtendWith(SpringExtension.class)
	public class Test2 {
		@Autowired
		UserService userService;
		@DisplayName("Test Spring @Autowired Integration")
		@Test //注意这里可以没有public
		void testGet() {
			userService.printName();
			//assertEquals("UserServiceImpl", userService.printName());
		}

	}

2.springboot2.2.0之后,spring-boot-starter-test默认支持的是junit5;
我这里的测试环境:spring boot2.2.0+maven3.5.4+jdk8+idea2019.1.3

  2.1junit4的测试:
	需要的的依赖:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-junit-jupiter</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <!-- 不用指定版本<version>4.12</version> -->
    </dependency>
</dependencies>
	
	测试类举例:
	import com.example.service.UserService;
	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;

	/**
	 * Created by Administrator on 2020/1/31 15:03
	 */
	@SpringBootTest
	@RunWith(SpringRunner.class)
	public class Test2 {

		@Autowired
		UserService userService;

		@Test  //注意 public
	  public  void contextLoads() {
			userService.printName();
		}
	}

	 

  2.2junit5的测试:
	需要的的依赖:
		<dependencies>
			<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>
		</dependencies>

		<build>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
			</plugins>
		</build>
	
	
	测试类举例:

	import com.example.service.UserService;
	import org.junit.jupiter.api.DisplayName;
	import org.junit.jupiter.api.Test;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.boot.test.context.SpringBootTest;

	@SpringBootTest
	public  class DemoYsy2ApplicationTests {

		@Autowired
		UserService userService;

		@DisplayName("Test Spring @Autowired Integration")
		@Test
		void contextLoads() {
			userService.printName();
		}

	}

3.两个版本的依赖声明对比:

2.1.0的spring-boot-dependencies

 <junit.version>4.12</junit.version>
 <junit-jupiter.version>5.3.1</junit-jupiter.version>

 <dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>${junit.version}</version>
</dependency>

 <dependency>
	<groupId>org.junit</groupId>
	<artifactId>junit-bom</artifactId>
	<version>${junit-jupiter.version}</version>
	<type>pom</type>
	<scope>import</scope>
</dependency>
  <dependency>
	<groupId>org.mockito</groupId>
	<artifactId>mockito-junit-jupiter</artifactId>
	<version>${mockito.version}</version>
</dependency>

  <maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
  <plugin>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>${maven-surefire-plugin.version}</version>
  </plugin>




2.2.0的spring-boot-dependencies
  <junit.version>4.12</junit.version>
  <junit-jupiter.version>5.5.2</junit-jupiter.version>

 <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
      </dependency>

  <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>${junit-jupiter.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>${mockito.version}</version>
      </dependency>


 <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>${maven-surefire-plugin.version}</version>
 </plugin>

附加:
版本新特性:
2.1.0版本新特性:https://www.xttblog.com/?p=3299
2.2.0版本新特性:https://zhuanlan.zhihu.com/p/95545254
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.2-Release-Notes
junit5支持情况:https://cloud.tencent.com/developer/article/1522208

注意点:
1.Junit4中为org.junit.Test,而Junit5中为org.junit.jupiter.api.Test
2.maven3.6.3,截止目前(2020年1月31日17:05:24)
在IDEA使用会有点问题;建议使用之前的版本

后话:JUnit5和Mockito将是以后的单元测试标配;
上面只是简单的把依赖关系说明了一下,具体的测试使用还待学习。。
参考:
https://www.jianshu.com/p/0eb2dfea55b4

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章