在 Gradle 中使用 JUnit 首先,你必須在 build.gradle 中增加 'java' 這個 plugin 我們要使用 JUnit 4 來撰寫單元測試,因此要在 dependencies 中定義 testCompile: 並設定task test 觸發junit apply plugin: 'java' dependencies { testCompile 'junit:junit:4.+' } tasks.named('test') { useJUnitPlatform() } 在src/test/java 下放你的測試檔案 主要是看是否符合預期 Assertions.assertEquals(matches, true); 如下 ![image](https://hackmd.io/_uploads/rJZbDVd8p.png) 接著要運行測試,可以執行 gradle test 指令,如果測試成功的話,會顯示以下的畫面: ![image](https://hackmd.io/_uploads/H1PVI4dIa.png) 在 build/reports/tests 中,也會產生一份 HTML 報表: ![image](https://hackmd.io/_uploads/HyKILE_UT.png)