我不想再使用 powermock 了。因為 junit5 開始模擬靜態類。所以我試圖擺脫 powermock 方法。
如您所知,您可以使用 whenNew 關鍵字創建類的實體。
在Junit5 中有什么替代whenNew 的方法嗎?
這是我的代碼的一部分:
whenNew(PDFDocument.class).withNoArguments().thenReturn(pdfDocument);
whenNew(PSConverter.class).withNoArguments().thenReturn(converter);
doNothing().when(pdfDocument).load(ArgumentMatchers.any(ByteArrayInputStream.class));
doAnswer(invocationOnMock -> {
ByteArrayOutputStream outputStream = invocationOnMock.getArgument(1);
outputStream.write(content);
return outputStream;
}).when(converter).convert(ArgumentMatchers.any(), ArgumentMatchers.any(ByteArrayOutputStream.class));
uj5u.com熱心網友回復:
根據檔案,自 Mockito 3.5.0 起可使用模擬物件構造。
首先,您需要將mockito-inline而不是添加mockito-core到您的測驗依賴項中。
mockito-inline提供模擬靜態或最終方法、建構式的能力。mockito-core 與 mockito-inline 之間的區別
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
讓我們創建一個簡單的服務來測驗哪些實體化了物件。
public class A {
private final String test;
public A(String test) {
this.test = test;
}
public String check() {
return "checked " this.test;
}
}
public class B {
private String check = " B check ";
public String check() {
return check;
}
}
public class TestService {
public String purchaseProduct(String param) {
A a = new A(param);
B b = new B();
return a.check() b.check();
}
}
帶有注釋的建構式模擬示例:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import static org.mockito.Mockito.when;
public class ConstructorMockTest {
@Test
public void test_mocked_construction() {
try (
//create mock for object A
MockedConstruction<A> mockedA = Mockito.mockConstruction(A.class,
(mock, context) -> {
//set return value for object A mock methods
when(mock.check()).thenReturn(" Constructor Mock A ");
});
//create mock for object B
MockedConstruction<B> mockedB = Mockito.mockConstruction(B.class,
(mock, context) -> {
//set return value for object B mock methods
when(mock.check()).thenReturn(" Constructor Mock B ");
}))
{
// every A object creation is current try() scope returning a mock
A aObject = new A("test");
Assertions.assertEquals( aObject.check(), " Constructor Mock A ");
// every B object creation is current try() scope returning a mock
B bObject = new B();
Assertions.assertEquals( bObject.check(), " Constructor Mock B ");
//Example of testing service which creates A and B objects
TestService service = new TestService();
String serviceResult = service.purchaseProduct("test");
Assertions.assertEquals(serviceResult, " Constructor Mock A Constructor Mock B ");
}
}
}
對于您的課程示例:
@Test
public void test() {
byte[] content = new byte[] {1,1};
try (
MockedConstruction<PDFDocument> mockedPDFDocument = Mockito.mockConstruction(PDFDocument.class,
(mock, context) -> {
doNothing().when(mock).load(ArgumentMatchers.any(ByteArrayInputStream.class));
});
MockedConstruction<PSConverter> mockedPSConverter = Mockito.mockConstruction(PSConverter.class,
(mock, context) -> {
doAnswer(invocationOnMock -> {
ByteArrayOutputStream outputStream = invocationOnMock.getArgument(1);
outputStream.write(content);
return outputStream;
}).when(mock).convert(ArgumentMatchers.any(), ArgumentMatchers.any(ByteArrayOutputStream.class));
}))
{
//call services which instantiates PDFDocument and PSConverter
PDFDocument pdfDocument = new PDFDocument();
PSConverter psConverter = new PSConverter();
Assertions.assertTrue(org.mockito.internal.util.MockUtil.isMock(pdfDocument));
Assertions.assertTrue(org.mockito.internal.util.MockUtil.isMock(psConverter));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/479447.html
上一篇:監視沒有PowerMock的課程
下一篇:如何解決這個錯誤org.hibernate.hql.internal.ast.QuerySyntaxException:unexpectedtoken:%
