在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 物聯(lián)網(wǎng)/ 單元測試
依賴管理實戰(zhàn)
配置遠程倉庫
安裝Gradle
簡介
項目自動化簡介
配置子項目
Gradle強大的特性
自動化測試
簡介
掌握構(gòu)建生命周期
使用命令行操作
構(gòu)建Java項目
管理任務
構(gòu)建工具
連續(xù)傳遞的特性
用Gradle開發(fā)Web項目
聲明依賴
構(gòu)建塊
簡介
測試Java應用
java構(gòu)建工具
為什么選擇Gradle
拆分項目文件
單元測試
多項目打包
Gradle 起步
介紹這個Gradle項目
簡介
自定義腳本
Gradle包裝器
簡要概述依賴管理
項目模塊化

單元測試

作為一個Java開發(fā)者,你有很多個測試框架可選,這一節(jié)我將介紹傳統(tǒng)的JUnit和TestNG,如果你沒有接觸過這些框架,你可以先看看他們的在線文檔。

使用JUnit

你將給你之前的ToDo應用的存儲類InMemoryToDoRepository.java編寫單元測試,為了突出不同框架的相同和不同之處,所有的單元測試都會驗證同一個類的功能。接下來你給子項目repository編寫測試,放置測試代碼的正確位置是在測試的標準布局里,在src/test/java目錄下創(chuàng)建一個名叫InMemoryToDoRepositoryTest.java的類,你可以學習測試驅(qū)動開發(fā)的相關理論,在代碼中添加適當?shù)臄嘌哉Z句,下面這段代碼用來測試插入功能的正確性。

    import com.manning.gia.todo.model.ToDoItem;
    import org.junit.Before;
    import org.junit.Test;
    import java.util.List;
    import static org.junit.Assert.*;

    public class InMemoryToDoRepositoryTest {
        private ToDoRepository inMemoryToDoRepository;
        //用這個注解標識的都會在類的所有測試方法之前執(zhí)行
        @Before
        public void setUp() {
            inMemoryToDoRepository = new InMemoryToDoRepository();
        }
        //用這個注解的都會作為測試用例
        @Test
        public void insertToDoItem() {
            ToDoItem newToDoItem = new ToDoItem();       
            newToDoItem.setName("Write unit tests");
            Long newId = inMemoryToDoRepository.insert(newToDoItem);        //錯誤的斷言會導致測試失敗 
            assertNull(newId);
            ToDoItem persistedToDoItem = inMemoryToDoRepository.findById(newId);
            assertNotNull(persistedToDoItem);
            assertEquals(newToDoItem, persistedToDoItem);
        }
    }

接下來你需要在依賴配置中添加JUnit的依賴:

    project(':repository')repositories {
        mavenCentral()
    }
    {
    }
    dependencies {
        compile project(':model')
        testCompile 'junit:junit:4.11'
    }

之前我們講過test任務會先編譯源代碼,生成Jar文件,然后編譯測試代碼最后執(zhí)行測試,下面的命令行輸出顯示了有一個斷言出錯的情況:

    $ gradle :repository:test
    :model:compileJava
    :model:processResources UP-TO-DATE
    :model:classes
    :model:jar
    :repository:compileJava
    :repository:processResources UP-TO-DATE
    :repository:classes
    :repository:compileTestJava
    :repository:processTestResources UP-TO-DATE
    :repository:testClasses
    :repository:test

    com.manning.gia.todo.repository.InMemoryToDoRepositoryTest
    > testInsertToDoItem FAILED//出錯方法的名字
    java.lang.AssertionError at InMemoryToDoRepositoryTest.java:24
    //測試結(jié)果概括
    1 test completed, 1 failed
    :repository:test FAILED

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':repository:test'.
    > There were failing tests. See the report at:
    ? file:///Users/ben/dev/gradle-in-action/code/chapter07/junit-test-
    ? failing/repository/build/reports/tests/index.html

從輸出可以看出一個斷言失敗了,這正是你想看到的,顯示的信息并沒有告訴你為什么測試失敗了,指示告訴你第24行的斷言失敗了,如果你有很多個測試,你需要打開測試報告才能找到出錯的原因,你可以在任務使用-i選項打印日志輸出:

    $ gradle :repository:test –i
    ...
    com.manning.gia.todo.repository.InMemoryToDoRepositoryTest
    > testInsertToDoItem FAILED
    java.lang.AssertionError: expected null, but was:<1>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotNull(Assert.java:664)
    at org.junit.Assert.assertNull(Assert.java:646)
    at org.junit.Assert.assertNull(Assert.java:656)
    at com.manning.gia.todo.repository.InMemoryToDoRepositoryTest
    ? .testInsertToDoItem(InMemoryToDoRepositoryTest.java:24)
    ...

在堆棧樹我們可以找到出錯的原因是newId的值我們假定是null的實際上為1,所以斷言出錯了,修改之后再運行可以看到所有測試都通過了:

    $ gradle :repository:test
    :model:compileJava
    :model:processResources UP-TO-DATE
    :model:classes
    :model:jar
    :repository:compileJava
    :repository:processResources UP-TO-DATE
    :repository:classes
    :repository:compileTestJava
    :repository:processTestResources UP-TO-DATE
    :repository:testClasses
    :repository:test

Gradle可以生成更加視覺化的測試報告,你可以在build/reports/test目錄下找到HTML文件,打開HTML文件你應該可以看到類似這樣的東西:

http://wiki.jikexueyuan.com/project/gradleIn-action/images/dag54.png" alt="" />

使用其他測試框架

你可能在你的項目中想使用其他的測試框架比如TestNG和Spock

使用testNG

比如你想用testNG來編寫相同的測試類,相似的,你用testNG指定的注解來標識相應的方法,要想你的構(gòu)建執(zhí)行testNG測試,你需要做兩件測試:

  1. 聲明對testNG庫的依賴
  2. 調(diào)用Test#useTestNG()方法來聲明測試過程使用testNG框架

如下圖所示來配置腳本文件:

    project(':repository'){
        repositories {
            mavenCentral()
        }
         
        dependencies {
            compile project(':model')
            testCompile 'org.testng:testng:6.8'
        }
        //設置使用testNG來測試
        test.useTestNG()
     
    }