简体   繁体   English

嵌套模拟中的方法上的Mockito doNothing

[英]Mockito doNothing on a method inside a nested mock

Suppose I have 3 classes, ClassA which contains a ClassB which contains a ClassC. 假设我有3个类,ClassA包含一个ClassB,ClassB包含一个ClassC。 I'm trying to test a method inside ClassA, that method needs to call ClassB to get the instance of ClassC and execute a VOID method inside ClassC (I know this wrong, cause ClassA should not be aware of ClassC, code smell, this is not my code and just trying to create a test of it). 我正在尝试在ClassA中测试一个方法,该方法需要调用ClassB来获取ClassC的实例并在ClassC中执行VOID方法(我知道这是错误的,因为ClassA不应该知道ClassC,代码有异味,这是而不是我的代码,而只是尝试对其进行测试)。 However when I try to skip that method call using Mockito.doNothing I get the following error: 但是,当我尝试使用Mockito.doNothing跳过该方法调用时,出现以下错误:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

Here's the code: 这是代码:

ClassA: A类:

    public class ClassA {
        private ClassB classB;

        public ClassA(ClassB classB) {
            this.classB = classB;
        }

        public void methodToTest() {
            classB.getClassC().someVoidMethod("123");
        }
    }

ClassB: B类:

    public class ClassB {
        private ClassC classC;

        public ClassB(ClassC classC) {
            this.classC = classC;
        }

        public ClassC getClassC() {
            return classC;
        }

    }

ClassC: ClassC:

    public class ClassC {
        public void someVoidMethod(String arg) {
            System.out.println(arg);
        }
    }

Test Class 测试班

    @RunWith(MockitoJUnitRunner.class)
    public class ClassATest {

        @InjectMocks
        private ClassA classA;

        @Mock(answer = Answers.RETURNS_DEEP_STUBS)
        private ClassB classB;

        @Mock
        private ClassC classC;

        @Test
        public void test() {
            Mockito.doNothing().when(classB.getClassC()).someVoidMethod("123");
        }    
    }

Once again, this code is not mine so I can't modify the way it uses the dependencies. 再说一次,这段代码不是我的,因此我无法修改它使用依赖项的方式。 I'm just trying to test it. 我只是想测试一下。

Quick Note: Mockito.when(...).thenReturn(...) works fine, but this is not my case since the method I'm trying to Mock is void. 快速说明:Mockito.when(...)。thenReturn(...)可以正常工作,但这不是我的情况,因为我要模拟的方法是无效的。

I think this works. 我认为这可行。 I am not sure why you can't use classB.getClassC() instead of referencing classC directly. 我不确定为什么不能使用classB.getClassC()而不是直接引用classC

@RunWith(MockitoJUnitRunner.class)
public class ClassATest {

  @InjectMocks
  private ClassA classA;

  @Mock(answer = Answers.RETURNS_DEEP_STUBS)
  private ClassB classB;

  @Mock
  private ClassC classC;

  @Before
  public void init() {
    doReturn(classC).when(classB).getClassC();
    doNothing().when(classC).someVoidMethod("123");
  }

  @Test
  public void test() {
    classA.methodToTest();
  }
}

Your mocking syntax is improperly arranged. 您的嘲笑语法安排不正确。

Mockito.doNothing().when(classB.getClassC()).someVoidMethod("123");

When you add spaces to this statement to individual pieces it is apparent. 当您将此语句的各个部分添加空格时,很明显。

Mockito      response                    method                  method
Mockito      .doNothing()     .when(classB.getClassC())    .someVoidMethod("123");

When you pass a METHOD to when, it expects a thenReturn When you pass an OBJECT to when, then you can chain a void method from that. 当您将thenReturn传递给when时,它会期望thenReturn当您将OBJECT传递给when时,您可以从中链接一个void方法。

Mockito.when(classB.getClassC()).thenReturn(classC);
Mockito.doNothing().when(classC).someVoidMethod("123");

The reason this doesn't work like you think it does is because classB.getClassC() doesn't work like a getter when passed through when() . 之所以不能像您认为的那样起作用,是因为classB.getClassC()在通过when()传递when()不像吸气剂那样起作用。

You're not actually passing a reference to the object to Mockito, but a method that Mockito will stub for you with thenReturn , which it complains is missing because you don't have one. 您实际上没有将对对象的引用传递给Mockito, 而是Mockito将使用thenReturn 为您存根的方法,它抱怨说它丢失了,因为您没有。

tl:dr; tl:dr;

Do not treat mock object methods like actual methods within mockito stubbing code. 在模拟存根代码中,请勿将模拟对象方法视为实际方法。

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

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