mockito中两种部分mock的实现,spy、callRealMethod

 

什么是类的部分mock(partial mock)?
A:部分mock是说一个类的方法有些是实际调用,有些是使用mockito的stubbing(桩实现)。java

 

为何须要部分mock?app

A:当须要测试一个组合方法(一个方法须要其它多个方法协做)的时候,某个叶子方法(只供别人调用,本身不依赖其它反复)已经被测试过,咱们其实不须要再次测试这个叶子方法,so,让叶子打桩实现返回结果,上层方法实际调用并测试。less

mockito实现部分mock的两种方式:spy和callRealMethod()eclipse

spy实现:

package spy;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.LinkedList;
import java.util.List;

import org.junit.Test;

public class SpyDemo {

    @Test
    public void spy_Simple_demo(){
        List<String> list = new LinkedList<String>();  
        List<String> spy = spy(list);  
        when(spy.size()).thenReturn(100);  
        
        spy.add("one");  
        spy.add("two");  
        
/*        spy的原理是,若是不打桩默认都会执行真实的方法,若是打桩则返回桩实现。
        能够看出spy.size()经过桩实现返回了值100,而spy.get(0)则返回了实际值*/
        assertEquals(spy.get(0), "one");  
        assertEquals(100, spy.size());  
    }
    
    @Test  
    public void spy_Procession_Demo() {  
        Jack spyJack = spy(new Jack());  
        //使用spy的桩实现实际仍是会调用stub的方法,只是返回了stub的值
        when(spyJack.go()).thenReturn(false);  
        assertFalse(spyJack.go()); 
        
        //不会调用stub的方法
        doReturn(false).when(spyJack).go();
        assertFalse(spyJack.go()); 
    } 
    
}



class Jack {  
    public boolean go() {  
        System.out.println("I say go go go!!");  
        return true;  
    }  
}

 

Spy类就能够知足咱们的要求。若是一个方法定制了返回值或者异常,那么就会按照定制的方式被调用执行;若是一个方法没被定制,那么调用的就是真实类的方法。工具

若是咱们定制了一个方法A后,再下一个测试方法中又想调用真实方法,那么只需在方法A被调用前,调用Mockito.reset(spyObject);就好了。测试

package spy;

import static org.mockito.Mockito.when;

import org.mockito.Mockito;

public class TestMockObject {

    public static void main(String[] args) {

        TestMockObject mock = Mockito.mock(TestMockObject.class);
        System.out.println(mock.test1());
        System.out.println(mock.test2());

        TestMockObject spy = Mockito.spy(new TestMockObject());
        System.out.println(spy.test1());
        System.out.println(spy.test2());

        when(spy.test1()).thenReturn(100);
        System.out.println(spy.test1());

        Mockito.reset(spy);
        System.out.println(spy.test1());
        System.out.println(spy.test2());

        when(spy.test1()).thenReturn(104);
        System.out.println(spy.test1());
    }

    public int test1() {
        System.out.print("RealTest1()!!! - ");
        return 1;
    }

    public int test2() {
        System.out.print("RealTest2()!!! - ");
        return 2;
    }

}

 

输出为:this

0
0
RealTest1()!!! - 1
RealTest2()!!! - 2
RealTest1()!!! - 100
RealTest1()!!! - 1
RealTest2()!!! - 2
RealTest1()!!! - 104

要注意的是,对Spy对象的方法定制有时须要用另外一种方法:
===============================================================================
Importantgotcha on spying real objects!

Sometimes it's impossible to usewhen(Object) for stubbing spies. Example:
List list = new LinkedList();
List spy = spy(list);

//Impossible: real method is called so spy.get(0) throwsIndexOutOfBoundsException (the list is yet empty)
when(spy.get(0)).thenReturn("foo");

//You have to use doReturn() for stubbing
doReturn("foo").when(spy).get(0);
===============================================================================
由于用when(spy.f1())会致使f1()方法被真正执行,因此就须要另外一种写法。spa

http://blog.csdn.net/dc_726/article/details/8568537.net

 

 

callRealMethod()实现

Use doCallRealMethod() when you want to call the real implementation of a method.设计

As usual you are going to read the partial mock warning: Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects. How does partial mock fit into this paradigm? Well, it just doesn't... Partial mock usually means that the complexity has been moved to a different method on the same object. In most cases, this is not the way you want to design your application.

However, there are rare cases when partial mocks come handy: dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.) However, I wouldn't use partial mocks for new, test-driven & well-designed code.

See also javadoc Mockito.spy(Object) to find out more about partial mocks. Mockito.spy() is a recommended way of creating partial mocks. The reason is it guarantees real methods are called against correctly constructed object because you're responsible for constructing the object passed to spy() method.

Example:

Foo mock = mock(Foo.class);
   doCallRealMethod().when(mock).someVoidMethod();

   // this will call the real implementation of Foo.someVoidMethod()
   mock.someVoidMethod();

See examples in javadoc for Mockito class

Returns:
stubber - to select a method for stubbing

 

package callRealMethod;

import org.junit.Test;
import static org.mockito.Mockito.*;

public class CallMethodDemo {
    @Test  
    public void callRealMethodTest() {  
        Jerry jerry = mock(Jerry.class);  
      
        doCallRealMethod().when(jerry).goHome();  
        doCallRealMethod().when(jerry).doSomeThingB();  
      
        jerry.goHome();  
      
        verify(jerry).doSomeThingA();  
        verify(jerry).doSomeThingB();  
    }  
}

class Jerry {  
    public void goHome() {  
        doSomeThingA();  
        doSomeThingB();  
    }  
  
    // real invoke it.  
    public void doSomeThingB() {  
        System.out.println("good day");  
  
    }  
  
    // auto mock method by mockito  
    public void doSomeThingA() {  
        System.out.println("you should not see this message.");  
  
    }  
}

  经过代码能够看出Jerry是一个mock对象, goHome()和doSomeThingB()是使用了实际调用技术,而doSomeThingA()被mockito执行了默认的answer行为(这里是个void方法,so,什么也不干)。

 

总结:

    spy和callrealmethod均可以实现部分mock,惟一不一样的是经过spy作的桩实现仍然会调用实际方法(我都怀疑这是否是做者的bug)。

   ★ 批注:spy方法须要使用doReturn方法才不会调用实际方法。

 

    mock技术是实施TDD过程必备的装备,熟练掌握mockito(或者其余工具)能够更有效的进行测试。虽然mockito做者也以为部分测试不是好的设计,可是在java这样一个不是彻底面向对象技术的平台上,咱们其实不必过度纠结这些细节,简洁,可靠的代码才是咱们须要的。

http://heipark.iteye.com/blog/1496603

相关文章
相关标签/搜索