顯示具有 JUnit5 標籤的文章。 顯示所有文章
顯示具有 JUnit5 標籤的文章。 顯示所有文章

2018年4月25日 星期三

常用方法、@DisplayName、@RepeatedTest、條件測試 (JUnit5 二)

※常用方法

@Test
public void xxx() {
    int a = 25;
    int b = 15;
    // Assertions.assertEquals(a, b);
    // Assertions.assertEquals(a, b, "我錯了");
    // Assertions.assertEquals(a, b, () -> "我死了");
    
    Assertions.assertEquals(a, b, () -> {
        if (a > b) {
            return "a大於b";
        }
        return "a小於b";
    });
}
    
@Test
public void ooo() {
    String rtn = Assertions.assertTimeout(Duration.ofMillis(1000), () -> {
        Thread.sleep(1500);
        System.out.println("超時也會印出");
        return "完成時印出";
    });
    
    //    String rtn = Assertions.assertTimeoutPreemptively(Duration.ofMillis(1000), () -> {
    //        Thread.sleep(1500);
    //        System.out.println("超時不會印出");
    //        return "完成時印出";
    //    });
    System.out.println(rtn);
}
    
@Test
public void zzz() {
    ArithmeticException ari = Assertions.assertThrows(ArithmeticException.class, () -> {
        int rtn = 1 / 0;
    });
    System.out.println(ari.getMessage());
}

※assertTimeoutPreemptively 的 preempt 有先發制人的意思,一 timeout 就結束了,所以後面的程式不會執行

※通常 overloading 方法,最後有一個 String,是錯誤時顯示在圖形裡的,JUnit4 是第一個參數,而 JUnit5 還可以用 java8 的 Supplier



※@DisplayName、@RepeatedTest

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
    
@DisplayName("這樣而已")
public class AppTest2 {
    @Test
    @RepeatedTest(3) // 加跑幾次
    public void xxx() {
        System.out.println("x");
    }
    
    @Test
    @DisplayName("哇哈哈")
    public void ooo() {
        System.out.println("o");
    }
}

※@RepeatedTest 裡的數字是「加跑」幾次的意思

※@DisplayName 要看圖形,如下:



※條件測試

@EnabledOnOs({ OS.WINDOWS, OS.LINUX })
// @DisabledOnOs({ OS.WINDOWS, OS.LINUX })
@EnabledIfSystemProperty(named = "user.language", matches = "en")
// @DisabledIfSystemProperty(named = "user.language", matches = "en")
@Test
public void testXxx() {
    System.out.println("xxx");
}

※可以使用 System.getProperties().list(System.out) 偷看一下

※目前版本是 5.1.1,還有一些 annotation 在實驗的階段

2018年4月24日 星期二

基本的 Annotation (JUnit5 一)

指南:官網、中文

※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 印出來是紅色的

※一樣都是 public void xxx(),或者 public static void xxx() 才可以測試

※注意以上的 annotation 除了 @Disabled 沒有繼承關係外,其他 5 個都有