国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Mockito Hello World

2019-11-14 23:42:53
字體:
來源:轉載
供稿:網友
Mockito Hello WorldMockito Hello World項目配置  IDE是Intellij IDEA,用gradle配置項目.  新建一個java項目,gradle中需要有這個:
repositories { jcenter() }dependencies { testCompile "org.mockito:mockito-core:1.+" }

單元測試用JUnit 4,所以gradle文件如下:
apply plugin: 'java'apply plugin: 'idea'sourceCompatibility = 1.5version = '1.0'repositories {    mavenCentral()    jcenter()}dependencies {    testCompile group: 'junit', name: 'junit', version: '4.11'    testCompile "org.mockito:mockito-core:1.8.5"}
  寫好之后在命令行執行:gradleidea  相關的包就會下載好,并出現在這里:Hello World程序  想著寫一個HelloWorld說明一下Mockito怎么用,結果寫著寫著就寫復雜了.  先看代碼:
package com.mengdd.examples.mockito;public class HelloWorld {    PRivate MyRobot mRobot;    public MyRobot getRobot() {        return mRobot;    }    public void setRobot(MyRobot robot) {        this.mRobot = robot;    }    /**     * This is the method we want to test.     * When there is an robot, this method return the robot's information     * otherwise, return some sorry text     */    public String sayHello() {        MyRobot robot = getRobot();        if (null != robot) {            return robot.getSomeInfo();        }        return "No robot here, sorry!";    }    /**     * MyRobot class     */    public static class MyRobot {        /**         * Get some information from somewhere, the implementation may varies         */        public String getSomeInfo() {            return "Hello World -- From robot";        }    }}
  這段代碼里面包含了一個HelloWorld類和一個內部靜態類MyRobot(我的機器人).  HelloWorld中有一個方法叫sayHello(),是我們要測的方法.  它會判斷成員變量是否為空,不為空則調用其方法獲取一些信息,否則返回一條提示信息.  -------------------  這里插播一下,關于內部類的介紹可以參見以前的一篇筆記:http://m.survivalescaperooms.com/mengdd/archive/2013/02/08/2909307.html  內部靜態類和內部非靜態類可以類比:靜態成員變量和非靜態成員變量.  -------------------  測試類代碼如下:
package com.mengdd.examples.mockito;import org.junit.Test;import static org.hamcrest.CoreMatchers.is;import static org.junit.Assert.assertThat;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.when;public class HelloWorldTest {    @Test    public void testSayHelloWhenThereIsRobot() throws Exception {        // We want to test the sayHello() method in the class HelloWorld.        // But we don't care about how MyRobot get its information.(We can treat it as external interface).        // Maybe the robot rely on many complicated things.(Although it's simple in this case.)        // And when the robot's actions change in the future, we don't need to change this test case.        // We can mock the MyRobot's action to make it simple and just test whether sayHello() can do its own work.        // Mock steps        HelloWorld.MyRobot robot = mock(HelloWorld.MyRobot.class); // Mock MyRobot class        String mockInfo = "some mock info";        when(robot.getSomeInfo()).thenReturn(mockInfo); // Set behavior for mock object        // real object        HelloWorld helloWorld = new HelloWorld();//This is the real objec we want to test        helloWorld.setRobot(robot);//set the mock robot to real object        // execute the target method we want to test        String result = helloWorld.sayHello();        // assert the result is what we want to have        assertThat(result, is(mockInfo));    }}
  因為我們要測試的是HelloWorld的sayHello()方法是否能正常工作.  我們需要假定其中robot的行為是正常的(我們并不關心robot實際上做的工作,以及它怎么做),所以這里用到了mock.  比如如果機器人的實現中要發送一個請求,我們這里就是直接mock它得到的結果,而不是真的去發這個請求.  就好像準備好了所需要的所有外部條件,看sayHello()方法的表現是否能符合我們的預期.  為了全面起見,測試了sayHello()的另一種case,這里沒有用到mockito.
@Testpublic void testSayHelloWhenThereIsNoRobot() throws Exception {    HelloWorld helloWorld = new HelloWorld();    helloWorld.setRobot(null);    String result = helloWorld.sayHello();    assertThat(result, is("No robot here, sorry!"));}
參考資料  官網:http://mockito.org/  官方文檔:http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html  Mockito at github: https://github.com/mockito/mockito  可以在這里看版本號:http://mvnrepository.com/artifact/org.mockito/
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 原平市| 泗洪县| 翼城县| 呼伦贝尔市| 阿荣旗| 通山县| 宣恩县| 遂溪县| 泰和县| 三门县| 绥德县| 汪清县| 锡林浩特市| 乐都县| 神木县| 清新县| 临泉县| 犍为县| 紫金县| 卓资县| 莱西市| 基隆市| 吴川市| 班玛县| 积石山| 江西省| 竹山县| 竹北市| 珲春市| 忻城县| 镇坪县| 绵竹市| 安岳县| 江津市| 新蔡县| 武山县| 武山县| 右玉县| 包头市| 唐山市| 阿巴嘎旗|