Now I want to use Junit 5 + Mockito 4.x version + Mockito-inline 4.x Version instead of Junit 4 + PowerMock 2.0.9
Because the Junit 5 doesn't support PowerMock also Mockito-inline can mock static, look like it doesn't need PowerMock anymore.
But when I use Mockito mock static, I want to use the same effect like Powermock.whenNew(xxx.class).withArgument(1,2,3,4).thanReturn(someThing).
This is part of my code and it can work.
@Test
void get_report_page() {
ReportPageRequest reportPageRequest = prepare_request();
prepare_reportPage(context, 9999L, pageable);
when(reportConverter.toReportSpecification(user, reportPageRequest)).thenReturn(reportSpecification);
when(PageRequest.of(1, 100)).thenReturn(pageRequest);
when(reportRepository.findAll(reportSpecification, pageRequest)).thenReturn(reportPage);
when(reportConverter.toReportPageResponse(context)).thenReturn(reportPageResponses);
pageMockedConstruction = Mockito.mockConstruction(PageImpl.class,
withSettings().useConstructor(reportPageResponses, pageable, 9999L), (mock, context) -> {
when(mock.getTotalElements()).thenReturn(123456L);
when(mock.getTotalPages()).thenReturn(1);
when(mock.getContent()).thenReturn(reportPageResponses);
});
Page<ReportPageResponse> actual = sut.getReportPage(user, reportPageRequest);
assertThat(actual.getTotalElements()).isEqualTo(123456L);
assertThat(actual.getTotalPages()).isEqualTo(1);
assertThat(actual.getContent()).isEqualTo(reportPageResponses);
}
}
And my question is I just can verify the mock static object behavior, but can't verify the result, this is my try
pageMockedConstruction = Mockito.mockConstruction(PageImpl.class,
withSettings().useConstructor(reportPageResponses, pageable, 9999L), (mock, context) -> {
when(mock.getTotalElements()).thenReturn(123456L);
when(mock.getTotalPages()).thenReturn(1);
when(mock.getContent()).thenReturn(reportPageResponses);
});
// I thought here will be the same mock object
// when expected and actual will throught the Mockito.mockConstruction, but actually generate the different object
PageImpl<ReportPageResponse> expected = new PageImpl<>(this.reportPageResponses, pageable, 9999L);
Page<ReportPageResponse> actual = sut.getReportPage(user, reportPageRequest);
// Here will be wrong, because actual and expected has different hashCode
Assertions.assertThat(actual).isEqualTo(expected);
I research so many articles, but I can't find the answer.
Have somebody encountered the same question?