英文:
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<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);
}
}
答案1
得分: 2
我在mockito文档中找到了原因:
> Mockito JUnit Runner仅在没有测试方法使用存根时触发UnnecessaryStubbingException异常。这意味着在“setup”方法或测试类构造函数中放置默认的存根是可以的。那个默认的存根需要至少被一个测试方法使用一次。
如果您不希望MyTest
类抛出异常,可以使用Mockito.lenient或MockitoSettings。但我想对于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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论