从 Jmockit 切换到 mockito
Switching from Jmockit to mockito
所以我决定从 jmockit 转移到 mockito,这对我来说很奇怪,我不明白 mockito 中有些东西是如何工作的
我有一个简单的@BeforeEach 方法,当我的对象被模拟时,我总是得到一个空指针异常
@Mock
public EntityManager entityManager;
@Mock
public TimerSessionBean timerSessionBean;
@Mock
public Client client;
private CaseSetReminder caseSetReminder;
private Request request;
private Message message;
@BeforeEach
final void beforeEach() {
request = new Request();
message = new Message();
String spaceId = "SPACE_ID";
String threadId = "THREAD_ID";
caseSetReminder = new CaseSetReminder();
caseSetReminder.entityManager = entityManager;
caseSetReminder.timerSessionBean = timerSessionBean;
ThreadM thread = new ThreadM();
thread.setName("spaces/" + spaceId + "/thread/" + threadId + "");
Sender sender = new Sender();
sender.setName("MyName");
message.setSender(sender);
message.setThread(thread);
Reminder reminder = new Reminder("Do Something", ZonedDateTime.now(ZoneId.of("Europe/Athens")).plusMinutes(10),
"DisplayName", "Europe/Athens", spaceId, threadId);
reminder.setReminderId(1);
timerSessionBean.nextReminderDate = reminder.getWhen();
}
它总是让我失望
Argument passed to verify() is of type TimerSessionBean 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();
但这不是真的,它的 timerSessionBean 是模拟的,我的语法是正确的
这就是我 运行 触发 beforeEach 方法的方法
@Test
void mockitoTest() throws Exception {
final String expectedDate = "12/12/2019 12:00 athens";
message.setText("remind me ' set next reminder Test' at " + expectedDate);
request.setMessage(message);
// Already set in mock a nextReminder that is to be in 10 mins from now()
//So this should not be set
caseSetReminder.setRequest(request);
caseSetReminder.setReminder();
//Verifies that setNextReminder is called 0 times because Input reminderDate is AFTER the current
verify(timerSessionBean , times(1)).setNextReminder(Mockito.any(Reminder.class), Mockito.any(ZonedDateTime.class));
}
希望大家帮我解决这个问题
您需要确保初始化 Mockito 引擎:
@BeforeEach
final void beforeEach() {
MockitoAnnotations.initMocks(this);
..
或
@RunWith(MockitoJUnitRunner.class)
public class TestClass{
此外,您不能像这样设置模拟 class 的字段:
timerSessionBean.nextReminderDate = reminder.getWhen();
尝试使用 doReturn()
、when()
、then()
配置行为
所以我决定从 jmockit 转移到 mockito,这对我来说很奇怪,我不明白 mockito 中有些东西是如何工作的
我有一个简单的@BeforeEach 方法,当我的对象被模拟时,我总是得到一个空指针异常
@Mock
public EntityManager entityManager;
@Mock
public TimerSessionBean timerSessionBean;
@Mock
public Client client;
private CaseSetReminder caseSetReminder;
private Request request;
private Message message;
@BeforeEach
final void beforeEach() {
request = new Request();
message = new Message();
String spaceId = "SPACE_ID";
String threadId = "THREAD_ID";
caseSetReminder = new CaseSetReminder();
caseSetReminder.entityManager = entityManager;
caseSetReminder.timerSessionBean = timerSessionBean;
ThreadM thread = new ThreadM();
thread.setName("spaces/" + spaceId + "/thread/" + threadId + "");
Sender sender = new Sender();
sender.setName("MyName");
message.setSender(sender);
message.setThread(thread);
Reminder reminder = new Reminder("Do Something", ZonedDateTime.now(ZoneId.of("Europe/Athens")).plusMinutes(10),
"DisplayName", "Europe/Athens", spaceId, threadId);
reminder.setReminderId(1);
timerSessionBean.nextReminderDate = reminder.getWhen();
}
它总是让我失望
Argument passed to verify() is of type TimerSessionBean 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();
但这不是真的,它的 timerSessionBean 是模拟的,我的语法是正确的
这就是我 运行 触发 beforeEach 方法的方法
@Test
void mockitoTest() throws Exception {
final String expectedDate = "12/12/2019 12:00 athens";
message.setText("remind me ' set next reminder Test' at " + expectedDate);
request.setMessage(message);
// Already set in mock a nextReminder that is to be in 10 mins from now()
//So this should not be set
caseSetReminder.setRequest(request);
caseSetReminder.setReminder();
//Verifies that setNextReminder is called 0 times because Input reminderDate is AFTER the current
verify(timerSessionBean , times(1)).setNextReminder(Mockito.any(Reminder.class), Mockito.any(ZonedDateTime.class));
}
希望大家帮我解决这个问题
您需要确保初始化 Mockito 引擎:
@BeforeEach
final void beforeEach() {
MockitoAnnotations.initMocks(this);
..
或
@RunWith(MockitoJUnitRunner.class)
public class TestClass{
此外,您不能像这样设置模拟 class 的字段:
timerSessionBean.nextReminderDate = reminder.getWhen();
尝试使用 doReturn()
、when()
、then()
配置行为