对于junit5.x和moockito 4.x,UnnecessaryStubbingsException
只会显示出来,
当您像这样将@ExtendWith(MockitoExtension.class)
添加到测试类中时:
@ExtendWith(MockitoExtension.class)
class MainTest {
private Service someService;
private MainService serviceUnderTest;
@BeforeEach
void setup() {
this.someService = Mockito.mock(Service.class);
this.serviceUnderTest = new MainService(someService);
}
//...see full code example below
}
但对于这种类型的测试来说,这@ExtendWith(MockitoExtension.class)
实际上并不是必需的.Mockitos JavaDoc说:
"初始化模拟并处理严格存根的扩展."所以,只要您不在
你的测试,你不会需要它的.
但如果你需要使用@ExtendWith(MockitoExtension.class)
然后您有几个选项:
You can replace
Mockito.doReturn(true).when(someService).execute(someFlag);
by
Mockito.lenient().doReturn(true).when(someService).execute(someFlag)
;
or alternatively add above the test class:
@MockitoSettings(strictness = Strictness.LENIENT)
和 your exception goes away again.
Or another option would be to use:
@Mock(strictness = Mock.Strictness.LENIENT)
private Service someService;
您还可以使用这个旧的、不推荐使用的注释选项.
@Mock(lenient = true) //deprecated!
Service someService;
正如安德烈亚斯·西格尔在上面的另一个答案中所建议的那样,
但正如我所说的,@Mock(lenient = true)
实际上是不受欢迎的.
您可以使用以下命令获取并运行完整的代码示例:
git clone --depth 1 --branch SO74233801 git@github.com:bodote/playground.git
cd backend
gradle test --tests '*MainTest*' --console plain
没有任何注释,因此没有显示UnnecessaryStubbingsException
的代码如下所示:
public interface Service {
public boolean execute(String flag) ;
}
和
public class MainService {
private Service someService;
public MainService(Service service) {
this.someService = service;
}
public boolean process(String flag) {
if (Objects.equals(flag, "flag1")) {
throw new IllegalArgumentException("Oh no, exception!");
}
boolean result = someService.execute(flag);
if (result) {
throw new IllegalArgumentException("Oh no, exception!");
}
return result;
}
}
当测试为:
class MainTest {
private Service someService;
private MainService serviceUnderTest;
@BeforeEach
void setup(){
this.someService = Mockito.mock(Service.class);
this.serviceUnderTest = new MainService(someService);
}
@ParameterizedTest
@MethodSource("getFlags")
void shouldTestIfFlagWorks(String someFlag) {
// Given
Mockito.doReturn(true).when(someService).execute(someFlag);
// When
Throwable thrown = Assertions.catchThrowable(() ->
serviceUnderTest.process(someFlag));
// Then
Assertions.assertThat(thrown).hasMessage("Oh no, exception!");
}
private static Stream<Arguments> getFlags() {
return Stream.of(
Arguments.of("flag1"),
Arguments.of("flag2")
);
}
}
试运行的鞋子编号为UnnecessaryStubbingsException
:
顺便说一句,我正在使用以下依赖项:
testImplementation 'org.mockito:mockito-core:4.8.1'
testImplementation 'org.mockito:mockito-junit-jupiter:4.8.1'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter', version: '5.9.1'
Now try adding @ExtendWith(MockitoExtension.class)
和 run the test again. The UnnecessaryStubbingsException
should show up now, until you either replace
Mockito.doReturn(true).when(someService).execute(someFlag);
by
Mockito.lenient().doReturn(true).when(someService).execute(someFlag)
;
or alternatively add above the test class:
@MockitoSettings(strictness = Strictness.LENIENT)
和 your exception goes away again.
I just also checked with
testImplementation group: 'org.mockito', name: 'mockito-all', version: '2.0.2-beta'
but each time with junit-jupiter 5.9.1
: same result.
Junit 4 however does have different annotations 和 behaves quite differently.