不能将when().thenReturn()用于mockbean,也可以使用doReturn().when()获得NPE
我有一个 junit 测试类,它使用 @MockBean 注释模拟几个类依赖项。
@ExtendWith(MockitoExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
@ActiveProfiles("test")
public class AuthServiceTest {
@InjectMocks private AuthService authService;
@MockBean UserService userService;
@MockBean ...... // other mocks
@Test
public void testVerifyAuth() {
User user = new User();
// setting user params
Mockito.lenient().when(userService.createUser(anyString(), anyLong(), anyString(), any(Integer.class)....).thenReturn(user));
}
}
userService 存根对于其中一种 UserService 方法工作正常,但对于另一种方法 (createUser),它会失败并出现以下错误:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:用户不能 由 toString() 返回 toString() 应该返回 String *** 如果您不确定为什么会出现上述错误,请继续阅读。由于语法的性质,可能会出现上述问题,因为:
- 此异常可能发生在错误编写的多线程测试中。请参阅 Mockito FAQ 有关并发限制的信息 测试。
- 使用 when(spy.foo()).then() 语法对间谍进行存根。存根间谍更安全 -
- 使用 doReturn|Throw() 系列方法。有关 Mockito.spy() 方法的更多信息,请参见 javadoc。
它建议使用 doReturn().when() 因为由于某种原因(可能存根未正确完成),它无法找到此存根。我也尝试使用这个,但是在调试期间,当在主 UserService.class 中实际调用此存根时,用户对象将返回为 null,而不是我试图创建的对象。
我在这里遗漏了什么吗?
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
所有其他依赖项都按预期正常工作
I have a junit test class that mocks couple of classes dependencies using @MockBean annotation.
@ExtendWith(MockitoExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
@ActiveProfiles("test")
public class AuthServiceTest {
@InjectMocks private AuthService authService;
@MockBean UserService userService;
@MockBean ...... // other mocks
@Test
public void testVerifyAuth() {
User user = new User();
// setting user params
Mockito.lenient().when(userService.createUser(anyString(), anyLong(), anyString(), any(Integer.class)....).thenReturn(user));
}
}
The userService stubbing is working fine for one of the UserService methods but for other one (createUser), it fails and getting this error:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue: User cannot
be returned by toString() toString() should return String
*** If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because:
- This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency
testing.- A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
It suggests to use doReturn().when() as due to some reason (maybe stubbing was not done properly), it's not able to find this stubbing. I tried to use this one too, but during debug, when this stubbing is actually called in main UserService.class, then the user object is returned as null instead of the object I was trying to create.
Is there anything I've been missing here?
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
All the other dependencies are working fine as expected
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据错误,它表示您从模拟方法返回了错误的类型。
userService.createUser
方法应返回String
类型,而您尝试返回User
类型。因此出现错误你的 createUser 方法的返回类型是什么?模拟应该返回相同的结果。假设创建用户为
模拟应该是
另外,如果
name
或age
为 NULL,在这里您也可以获得 NPE。确保模拟方法的参数没有为 null,或者如果预期为 null,则使用 Mockito.nullable(String.class) 等。As per the error it says you are returning the wrong type from the mocked method.
The method
userService.createUser
should return a type asString
where as you are trying to return the type asUser
. Hence the errorWhat is the return type of your createUser method? The mock should return the same. Assuming create user as
The mock should be
Also, here you can get NPE as well if either the
name
orage
is NULL. Either make sure that no parameter of the mocked method is null, or if its expected to have null then useMockito.nullable(String.class)
and so on.