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

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