Junit 5和Mockito 3:在通过构造函数注入@Mock时不会抛出UnnecessaryStubbingException

huangapple go评论96阅读模式
英文:

Junit 5 and Mockito 3: UnnecessaryStubbingException not thrown when injecting @Mock's through constructor

问题

以下是翻译好的部分:

观察下面的两个测试类,我认为它们的行为相同:都会抛出UnnecessaryStubbingException异常。然而MyTest2却没有抛出该异常。MockitoExtension类应默认将严格存根设置为严格模式。我似乎找不到关于这种行为的任何记录信息,而且我真的想要理解它。

我更喜欢像MyTest2这样编写我的测试,因为我喜欢在可能的情况下使用final字段,尽管我也非常喜欢Mockito执行的严格存根检查。请帮助我理解这两个测试之间的区别。

package example;

import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class MyTest {

  @Mock
  List<Integer> integerList;

  @Test
  void test() {
    Mockito.when(integerList.size()).thenReturn(10);

    System.out.println("Do something not involving list");

    Assertions.assertTrue(true);
  }
}

@ExtendWith(MockitoExtension.class)
class MyTest2 {

  final List<Integer> integerList;

  MyTest2(@Mock List<Integer> integerList) {
    this.integerList = integerList;
  }

  @Test
  void test() {
    Mockito.when(integerList.size()).thenReturn(10);

    System.out.println("Do something not involving list");

    Assertions.assertTrue(true);
  }
}
英文:

Looking at the two test classes below I assume the same behavior from them: to have an UnnecessaryStubbingException thrown. However MyTest2 does not. The MockitoExtension class should have strictness set to strict stubs by default. I can't seem to find any documented information about this behavior and I really want to understand it.

I prefer to write my tests as MyTest2 because I like to have final fields wherever possible, though I also really enjoy the strict stubs check done by Mockito.. Please help my understand the difference between the two tests.

package example;

import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class MyTest {

  @Mock
  List&lt;Integer&gt; integerList;

  @Test
  void test() {
    Mockito.when(integerList.size()).thenReturn(10);

    System.out.println(&quot;Do something not involving list&quot;);

    Assertions.assertTrue(true);
  }
}

@ExtendWith(MockitoExtension.class)
class MyTest2 {

  final List&lt;Integer&gt; integerList;

  MyTest2(@Mock List&lt;Integer&gt; integerList) {
    this.integerList = integerList;
  }


  @Test
  void test() {
    Mockito.when(integerList.size()).thenReturn(10);

    System.out.println(&quot;Do something not involving list&quot;);

    Assertions.assertTrue(true);
  }
}

答案1

得分: 2

我在mockito文档中找到了原因:

> Mockito JUnit Runner仅在没有测试方法使用存根时触发UnnecessaryStubbingException异常。这意味着在“setup”方法或测试类构造函数中放置默认的存根是可以的。那个默认的存根需要至少被一个测试方法使用一次。

如果您不希望MyTest类抛出异常,可以使用Mockito.lenientMockitoSettings。但我想对于MyTest2类来说,激活异常是不可能的。

英文:

I found the reason in the mockito documentation:

> Mockito JUnit Runner triggers UnnecessaryStubbingException only when none of the test methods use the stubbings. This means that it is ok to put default stubbing in a 'setup' method or in test class constructor. That default stubbing needs to be used at least once by one of the test methods.

If you do not want the exception to be thrown for the MyTest class, you can use Mockito.lenient or MockitoSettings. But I guess it is not possible to activate the exception for the MyTest2 class.

huangapple
  • 本文由 发表于 2020年10月2日 20:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64171526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定