Reputation: 954
so I decided to move from jmockit to mockito and it seems strange to me I can't understand how some things work in mockito
I have that simple @BeforeEach method and when my objects are mocked I always get a null pointer exception
@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();
}
it always throws me
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();
but that's not true its timerSessionBean is mocked and my syntax is correct
and that's the method I run that triggers the beforeEach method
@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));
}
I hope you guys can help me figure this out
Upvotes: 0
Views: 1145
Reputation: 26522
You need to make sure you init the Mockito engine:
@BeforeEach
final void beforeEach() {
MockitoAnnotations.initMocks(this);
..
or
@RunWith(MockitoJUnitRunner.class)
public class TestClass{
also, you cannot set fields of a mocked class like:
timerSessionBean.nextReminderDate = reminder.getWhen();
try using doReturn()
, when()
, then()
to configure behavior
Upvotes: 1