简体   繁体   English

使用Mockito测试代表方法

[英]Testing Delegate Method with Mockito

In the class like the below the only tests required around doActionOne() and doActionTwo() are to ensure that they delegate to doAction() with the correct parameters. 在如下所示的类中,围绕doActionOne()doActionTwo()进行的唯一测试仅是确保它们使用正确的参数委托给doAction()

As the delegate doAction(String a, int b, boolean c) method requires a lot of set-up then any solution should prevent the real method being called. 由于委托doAction(String a, int b, boolean c)方法需要进行大量设置,因此任何解决方案都应防止调用真实方法。

public class ClassUnderTest {

    public void doActionOne(String a, int b) {
        doAction(a, b, true);
    }

    public void doActionTwo(String a, int b) {
        doAction(a, b, false);
    }

    public void doAction(String a, int b, boolean c) {
        //already tested
    }
}

Such a test would seem to require some kind of partial mock or spy however I am unable to get this correct. 这样的测试似乎需要某种程度的模拟或间谍,但是我无法获得正确的结果。

The test should look something like the below although this approach doesn't work. 尽管这种方法行不通,但测试应类似于以下内容。

@Test
public void testDoActionOne(){
    ClassUnderTest cut = Mockito.mock(ClassUnderTest.class);

    //call the real method
    when(cut.doActionOne("A1", 1)).thenCallRealMethod();

    //test delegate called with correct params
    verify(cut, times(1)).doAction("A1", 1, true); //fails wanted but not invoked
}

Not sure if I need something like: 不知道我是否需要这样的东西:

http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/AdditionalAnswers.html#delegatesTo(java.lang.Object) http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/AdditionalAnswers.html#delegatesTo(java.lang.Object)

I know of two ways to do this. 我知道有两种方法可以做到这一点。 One means creating an anonymous inner class in which you override the method you don't want to test. 一种方法是创建一个匿名内部类,在该内部类中重写不需要测试的方法。 This does not involve any Mockito magic. 这不涉及任何Mockito魔术。 The other means using a Mockito Spy object, this is a proxy to a real instance, allowing you to specify stubbed behavior for some methods, and let the other methods fall through to the real instance. 另一种方法是使用Mockito Spy对象,它是真实实例的代理,允许您为某些方法指定存根行为,并使其他方法落入真实实例。

Method 1, using an anonymous inner class: 方法1,使用匿名内部类:

public class MyTest {

    private String a;
    private String b;
    private boolean c;

    private ClassUnderTest instance = new ClassUnderTest() {
        @Override
        public void doAction(String a, int b, boolean c) {
            MyTest.this.a = a; 
            MyTest.this.b = b;
            MyTest.this.c = c;
        }
    }

    public void test() {
        // SETUP
        String expectedA = "test value A";
        String expectedB = "test value B";
        boolean expectedC = true;

        // CALL
        instance.doActionOne(expectedA, expectedB);

        // VERIFY
        assertEquals(expectedA, a);
        assertEquals(expectedB, b);
        assertEquals(expectedC , c);
    }

Method 2, using Mockito spy object: 方法2,使用Mockito间谍对象:

@RunWith(MockitoJUnitRunner.class)
public class MyTest {

    @Spy
    private ClassUnderTest instance;

    public void test() {
        // SETUP
        String expectedA = "test value A";
        String expectedB = "test value B";
        boolean expectedC = true;
        doNothing().when(instance).doAction(expectedA , expectedB, expectedC);

        // CALL
        instance.doActionOne(expectedA, expectedB);

        // VERIFY
        verify(instance, times(1)).doAction(expectedA , expectedB, expectedC);
    }

Because a spy is a Mockito controlled proxy you can also verify if methods were called on a spy, which is what you need here. 因为间谍是Mockito控制的代理,所以您还可以验证是否在间谍上调用了方法,这就是您所需要的。 You could also specify a stubbed return value for doAction (if it wasn't a void method): 您还可以为doAction指定存根返回值(如果它不是void方法):

        // SETUP
        doReturn("stubbed value").when(instance).doAction(expectedA , expectedB, expectedC);

It goes like this: 它是这样的:

when(object.getFieldValue(attribute)).thenReturn(NESTED_ID);
verify(object, times(1)).getFieldValue(eq(attribute));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM