idea使用junit5

JUnit 5 对 Java 运行环境的最低要求是 Java 8。

IntelliJ IDEA支持自2016.2之后支持junit5。

推荐使用IDEA 2017.3或之后的版本。

各idea版本和支持的junit5版本对照:

《idea使用junit5》

junit5常用注解:

《idea使用junit5》

JUnit 5 提供了三种不同的方式来执行测试用例,分别是通过 Gradle 插件、Maven 插件和命令行来运行。

1.基于Gradle

新建build.gradle 文件,文件和pom文件同一级,文件内容:

buildscript {

repositories {

mavenCentral()

}

dependencies {

classpath’org.junit.platform:junit-platform-gradle-plugin:1.0.2′

  }

}

repositories {

mavenCentral()

}

ext.junit4Version        =’4.12′

ext.junitVintageVersion  =’4.12.2′

ext.junitPlatformVersion =’1.0.2′

ext.junitJupiterVersion  =’5.0.2′

ext.log4jVersion        =’2.9.0′

applyplugin:’java’

applyplugin:’eclipse’

applyplugin:’idea’

applyplugin:’org.junit.platform.gradle.plugin’

jar {

baseName =’junit5-gradle-consumer’

  version =’1.0.0-SNAPSHOT’

}

compileTestJava {

sourceCompatibility =1.8

  targetCompatibility =1.8

  options.compilerArgs +=’-parameters’

}

junitPlatform {

// platformVersion ‘1.0.2’

  filters {

engines {

// include ‘junit-jupiter’, ‘junit-vintage’

// exclude ‘custom-engine’

      }

tags {

// include ‘fast’

        exclude’slow’

      }

// includeClassNamePattern ‘.*Test’

  }

// configurationParameter ‘junit.jupiter.conditions.deactivate’, ‘*’

// enableStandardTestTask true

// reportsDir file(‘build/test-results/junit-platform’) // this is the default

  logManager’org.apache.logging.log4j.jul.LogManager’

}

dependencies {

// JUnit Jupiter API and TestEngine implementation

  testCompile(“org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}”)

testRuntime(“org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}”)

// If you also want to support JUnit 3 and JUnit 4 tests

  testCompile(“junit:junit:${junit4Version}”)

testRuntime(“org.junit.vintage:junit-vintage-engine:${junitVintageVersion}”)

// To avoid compiler warnings about @API annotations in JUnit code

  testCompileOnly(‘org.apiguardian:apiguardian-api:1.0.0’)

// To use Log4J’s LogManager

  testRuntime(“org.apache.logging.log4j:log4j-core:${log4jVersion}”)

testRuntime(“org.apache.logging.log4j:log4j-jul:${log4jVersion}”)

// Only needed to run tests in an (IntelliJ) IDE(A) that bundles an older version

  testRuntime(“org.junit.platform:junit-platform-launcher:${junitPlatformVersion}”)

}

task wrapper(type: Wrapper) {

description =’Generates gradlew[.bat] scripts’

  gradleVersion =’4.3.1′

}

基于maven

pom文件添加:

传的图片,复制代码编辑器粘贴不上,真是醉醉的

《idea使用junit5》
《idea使用junit5》

写个测试类

import org.junit.jupiter.api.AfterAll;

import org.junit.jupiter.api.AfterEach;

import org.junit.jupiter.api.BeforeAll;

import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.Disabled;

import org.junit.jupiter.api.DisplayName;

import org.junit.jupiter.api.Test;

public class JUnit5UserRepositoryImplFileTest {

@BeforeAll

  static void setup() {

// Initialize connection to file.

    System.out.println(“@BeforeAll – Execute once before all test methods in this class.”);

}

@BeforeEach

  void init() {

// Insert some sample data before each test

    System.out.println(“@BeforeEach – Executed before each test method in this class.”);

}

@DisplayName(“Test add user successfully.”)

@Test

  void testAddUserSuccess() {

System.out.println(“Test add user successfully”);

}

@DisplayName(“Test add user with passed argument is null.”)

@Test

  void testAddUserNull() {

System.out.println(“Test add null user.”);

}

@Test

  @Disabled(“Not implemented yet.”)

void testDeleteUser() {}

@AfterEach

  void tearDown() {

// Reset the file content.

    System.out.println(“@AfterEach – This method is called after each test method.”);

}

@AfterAll

  static void done() {

// Closes connection to the file

    System.out.println(“@AfterAll – This method is called after all test methods.”);

}

}

运行JUnit 5测试

使用Maven运行JUnit 5测试

mvn test

用Gradle运行JUnit 5测试

gradle test

用idea运行JUnit 5测试

IntelliJ默认支持JUnit 5。因此,在IntelliJ上运行JUnit 5非常简单,只需右键单击 – >运行或Ctrl-Shift-F10

《idea使用junit5》

    原文作者:yiteng595
    原文地址: https://www.jianshu.com/p/d04078355afc
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞