zoukankan      html  css  js  c++  java
  • Mockito Hello World

    Mockito 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.5
    version = '1.0'
    
    repositories {
        mavenCentral()
        jcenter()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.11'
        testCompile "org.mockito:mockito-core:1.8.5"
    }
    
    
      写好之后在命令行执行:gradle idea
      相关的包就会下载好,并出现在这里:
     
     

    Hello World程序

      想着写一个Hello World说明一下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://www.cnblogs.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.
    @Test
    public 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/
      Mockito at github: https://github.com/mockito/mockito
      可以在这里看版本号: http://mvnrepository.com/artifact/org.mockito/
     
  • 相关阅读:
    emacs 编程入门 函数
    boost库介绍
    jquery css attr
    html 相对定位 绝对 定位 css + div
    我常用的grep
    我常用的tar
    C#皮肤的用法(皮肤资源+使用实例下载)
    C#资源文件操作示例 创建资源和读取资源
    黄聪:c#中高效的excel导入sqlserver的方法
    黄聪:Delphi 中的 XMLDocument 类详解(2) 记要
  • 原文地址:https://www.cnblogs.com/mengdd/p/4415362.html
Copyright © 2011-2022 走看看