【发布时间】:2021-05-27 17:17:24
【问题描述】:
我有一个类似的东西:
class User {
String name;
}
class Contacts {
User getUser() {
return new User();
}
}
我这样做是为了在我的测试中可以模拟如下方法:
@ExtendWith(MockitoExtension.class)
class ContactsTest {
@Spy
private Contacts sut;
@Mock
private User user;
@Test
void testSomething() {
doReturn(user).when(sut).getUser();
}
@Test
void testGetUser() {
// verify(new User(), times(1));
// verify(user, times(1));
}
}
我如何测试testGetUser?
上面评论的我仅有的两个想法给了我这些错误:
对于第一个:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type User and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
第二个
org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
【问题讨论】:
标签: java junit mockito junit-jupiter