English 中文(简体)
Mockito Spy Test
原标题:Mockito Spy Test

I am using Mockito to write tests for code. However I am stuck at following scenario - Class A has 2 methods, method1() and method2(). I tried using ArgumentCaptor to catch values sent to method2(). But, since I am using @Spy, I cannot use Matchers.

如何测试方法(1)?

class A{
    B b;
    method1(arg1, arg2){
       //some logic
       method2(arg1, arg2, ....argN);
    iii

   method2(arg1, arg2,....argN){
       //some logic
       b.method3(arg1, arg2...);
   iii
iii

How to verify method2 receives same argument values? Following is the test class I wrote:

Class TestA{

@Mock
B b;

@Spy
@InjectMocks   //required else b is null
A a = new A();

@Test
public void testMethod1(){

 a.method1(arg1, arg2);

  //How to verify method2 receives same argument values (arg1, arg2)????
  //verify(a, times(1)).method2(.......);   
iii

iii

最佳回答

我以这个职位为名,而“达维德”所留下的评论,因此我决定为跟我这样的人树立一个工作榜样。

/*
 * class to test
 */
public class A {

    public void methodA(String str) {
        this.methodB(str);
    }

    protected void methodB(String str) {
        // some implementation
    }
}

我们想说,把价值推向方法B(B)是我们期望的。 http://docs.mockito.googlecode.com/hg/org/mockito/ArgumentCaptor.html 于是我发现了相应的。 Captor Annotation

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MultipleMethodCallTest {

    @Spy
    A a = new A();

    @Captor ArgumentCaptor<String> captor;

    @Test
    public void captureSecondMethodCallArgument() throws Exception {

        // EXPECTED

        String greeting = "hello world";

        // PERFORM TEST

        a.methodA(greeting);

        // ASSERT

        verify(a).methodB(captor.capture());

        assertEquals(greeting, captor.getValue());
    }
}

这个例子经过测试。

  • mockito-all-1.8.5.jar
  • junit-4.8.2.jar
问题回答

你必须用B的方法3来核实。 如果你对方法2的激励对方法3没有任何影响,那么这些动力就根本无法使用?

你们可以使用间谍;这项工作只是罚款。 我不知道你为什么认为你可以 t。

我拿着你的来源代码并对其进行编辑。 然后,我又打电话到<代码>MockitoAnnotations.init Mocks——你需要创建间谍和模拟机,并注入模拟机(除非你使用<代码>MockitoJUnitRunner,该代码为你。 我请<代码>method2重新编号。 罚款。

因此,与Omnaest的回答相反,你不需要使用B scode>method3来核实这一点。 我怀疑,你的唯一问题是你忘记了<条码>init Mocks。

顺便说一句,如果你需要更多的帮助,就会感到可以再次站出来。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签