現在我想使用 Junit 5 Mockito 4.x 版本 Mockito-inline 4.x 版本而不是 Junit 4 PowerMock 2.0.9
因為 Junit 5 不支持 PowerMock 并且 Mockito-inline 可以模擬靜態,看起來它不再需要 PowerMock 了。
但是當我使用 Mockito 模擬靜態時,我想使用與 Powermock.whenNew(xxx.class).withArgument(1,2,3,4).thanReturn(someThing) 相同的效果。
這是我的代碼的一部分,它可以作業。
@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);
}
}
我的問題是我只能驗證模擬靜態物件的行為,但無法驗證結果,這是我的嘗試
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);
我研究了很多文章,但我找不到答案。
有人遇到過同樣的問題嗎?
uj5u.com熱心網友回復:
Powermock.whenNew和之間的主要區別在于,每次在呼叫建構式時實體化新物件時都會創建一個新的模擬Mockito.mockConstruction。Mokito但Powermock.whenNew可以配置為始終回傳一個模擬來構造多個物件。
根據檔案:
表示所表示型別的任何物件構造的模擬。在模擬構造的范圍內,任何攔截器的呼叫都將生成一個模擬,該模擬將在生成此范圍時按照指定的方式進行準備。也可以通過此實體接收模擬。
您可以使用MockedConstruction<T>.constructed()在背景關系中獲取所有生成的模擬。它們可用于驗證。
檢查行為的測驗示例:
public class A {
private final String test;
public A(String test) {
this.test = test;
}
public String check() {
return "checked " this.test;
}
}
public class TestService {
public String purchaseProduct(String param) {
A a = new A(param);
return a.check();
}
}
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import static org.mockito.Mockito.*;
public class ConstructorMockTest {
private MockedConstruction<A> mockAController;
@BeforeEach
public void beginTest() {
//create mock controller for all constructors of the given class
mockAController = Mockito.mockConstruction(A.class,
(mock, context) -> {
//implement initializer for mock. Set return value for object A mock methods
//this initializer will be called each time during mock creation
when(mock.check()).thenReturn(" Constructor Mock A ");
});
}
@Test
public void test() {
//each instantiation of class A will return new mock, which initialized by initializer from beginTest method
//new mock will be stored to mockAController.constructed() collection of mocks
A aObject = new A("test");
//ensure that method check() returns mocked value
Assertions.assertEquals(aObject.check(), " Constructor Mock A ");
//get just created mock for class A from controller. It will be first element of mockAController.constructed() collection
A aMock = mockAController.constructed().get(0);
//ensure that we get correct mock from mock controller, that it is equal from new created object
Assertions.assertEquals(aMock, aObject);
//verify that check method was executed on Mock
verify(aMock, times(1)).check();
//create new A object, new mock created and stored to mockAController.constructed()
A aObject2 = new A("test");
//ensure that method check() returns mocked value
Assertions.assertEquals(aObject2.check(), " Constructor Mock A ");
//get just created mock for class A from controller, it will be second object from constructed collection
A aMock2 = mockAController.constructed().get(1);
//ensure that we get correct mock from mock controller, that it is equal from just created A object
Assertions.assertEquals(aObject2, aMock2);
//verify that check method was executed on Mock
verify(aMock2, times(1)).check();
//Example of testing service which creates A object
TestService service = new TestService();
String serviceResult = service.purchaseProduct("test");
//ensure that service returned value from A mock
Assertions.assertEquals(serviceResult, " Constructor Mock A ");
//get just created mock for class A from controller, it will be third object from constructed collection
A aMock3 = mockAController.constructed().get(2);
//verify that check method was executed on Mock
verify(aMock3, times(1)).check();
}
@AfterEach
public void endTest() {
mockAController.close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/484658.html
