简体   繁体   English

如何在java中模拟单个方法

[英]How to mock a single method in java

Is it possible to Mock a single method of a Java class?是否可以模拟 Java 类的单个方法?

For example:例如:

class A {
    long method1();
    String method2();
    int method3();
}


// in some other class
class B {
    void someMethod(A a) {
       // how would I mock A.method1(...) such that a.method1() returns a value of my
       // choosing;
       // whilst leaving a.method2() and a.method3() untouched.
    }
}

Use Mockito's spy mechanism:使用Mockito's间谍机制:

A a = new A();
A aSpy = Mockito.spy(a);
Mockito.when(aSpy.method1()).thenReturn(5l);

The use of a spy calls the default behavior of the wrapped object for any method that is not stubbed.使用 spy 会为任何未存根的方法调用包装对象的默认行为。

Mockito.spy() / @Spy Mockito.spy() / @Spy

Use the spy() method from Mockito , and mock your method like this:使用Mockito 中spy()方法,并像这样模拟你的方法:

import static org.mockito.Mockito.*;

...

A a = spy(new A());
when(a.method1()).thenReturn(10L);

Based on Mockito's documentation the accepted answer is not the correct way to Spy real objects.根据 Mockito 的文档,接受的答案不是间谍真实对象的正确方法。

The right way to do this is to use the following pattern:正确的方法是使用以下模式:
doReturn("foo").when(spy).get(0);

Below you can find the snippet from Mockito's about Spying on real objects .您可以在下面找到 Mockito 的关于对真实物体进行间谍活动的片段。

Important gotcha on spying real objects!监视真实物体的重要问题!

Sometimes it's impossible or impractical to use when(Object) for stubbing spies.有时,将 when(Object) 用于 stubbing 间谍是不可能或不切实际的。 Therefore when using spies please consider doReturn|Answer|Throw() family of methods for stubbing.因此,在使用 spies 时,请考虑使用 doReturn|Answer|Throw() 系列方法进行 stubbing。 Example:例子:

 List list = new LinkedList(); List spy = spy(list); //Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty) when(spy.get(0)).thenReturn("foo"); //You have to use doReturn() for stubbing doReturn("foo").when(spy).get(0);

Mockito does not delegate calls to the passed real instance, instead it actually creates a copy of it. Mockito不会将调用委托给传递的真实实例,而是实际创建它的副本。 So if you keep the real instance and interact with it, don't expect the spied to be aware of those interaction and their effect on real instance state.因此,如果您保留真实实例并与之交互,则不要指望被监视的人会知道这些交互及其对真实实例状态的影响。 The corollary is that when an unstubbed method is called on the spy but not on the real instance , you won't see any effects on the real instance.推论是,当在 spy不是在真实实例上调用unstubbed方法时,您将看不到对真实实例的任何影响。 Watch out for final methods.注意最后的方法。 Mockito doesn't mock final methods so the bottom line is: when you spy on real objects + you try to stub a final method = trouble. Mockito 不模拟 final 方法,所以底线是:当您监视真实对象时 + 尝试存根 final 方法 = 麻烦。 Also you won't be able to verify those method as well.您也将无法验证这些方法。

Assuming you are using jmockit:假设您使用的是 jmockit:

public void testCase(@Mocked("methodToBeMocked") final ClassBoBeMocked mockedInstance) {
           new Expectations() {{
                   mockedInstance.methodToBeMocked(someParameter); returns(whateverYouLikeItToReturn);
           }}

   mockedInstance.callSomemethod();
}

您可以简单地创建一个A的子类来覆盖method1()

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

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