※Maven 設定
<properties>
<junit.platform.version>5.1.1</junit.platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
※尤於 JUnit5 只支援 java8 以上,所以 eclipse 要按專案右鍵內容,然後如下設定
※IntelliJ 預設會抓最高版本,想改的話,如下圖:
※gradle 設定
test {
systemProperty 'junit.jupiter.conditions.deactivate', '*'
systemProperties = [
'junit.jupiter.extensions.autodetection.enabled': 'true',
'junit.jupiter.testinstance.lifecycle.default': 'per_class'
]
}
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:5.1.1")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.1")
}
※
※測試
@BeforeAll
public static void start1() {
System.err.println("class之前執行1");
}
@BeforeAll
public static void start2() {
System.err.println("class之前執行2");
}
@BeforeEach
public void begin1() {
System.out.println("方法之前執行1");
}
@BeforeEach
public void begin2() {
System.out.println("方法之前執行2");
}
@AfterEach
public void end1() {
System.out.println("方法之後執行1");
}
@AfterEach
public void end2() {
System.out.println("方法之後執行2\r\n");
}
@AfterAll
public static void theEnd1() {
System.err.println("class之後執行1");
}
@AfterAll
public static void theEnd2() {
System.err.println("class之後執行2");
}
@Test
public void bruceTest1() {
System.out.println("測試1");
}
@Test
public void bruceTest2() {
System.out.println("測試2");
}
@Disabled
@Test
public void bruceTest3() {
System.out.println("測試3");
}
※結果:
class之前執行1
class之前執行2
方法之前執行1
方法之前執行2
測試1
方法之後執行1
方法之後執行2
方法之前執行1
方法之前執行2
測試2
方法之後執行1
方法之後執行2
class之後執行1
class之後執行2
※JUnit4 和 JUnit5 對應:
@Test--------------@Test
@Before-----------@BeforeEach
@After-------------@AfterEach
@BeforeClass-----@BeforeAll
@AfterClass-------@AfterAll
@Ignore------------@Disabled
※除了 @Disabled 外,其他的都沒有屬性了
※@BeforeAll 和 @AfterAll 印出來是紅色的
※@BeforeAll 和 @AfterAll 印出來是紅色的
※一樣都是 public void xxx(),或者 public static void xxx() 才可以測試
※注意以上的 annotation 除了 @Disabled 沒有繼承關係外,其他 5 個都有


沒有留言:
張貼留言