PowerMockito 模拟单个静态方法和另一个静态方法中的 return 对象
PowerMockito mock single static method and return object inside another static method
我已经编写了测试用例来模拟静态 classes 和使用 PowerMockito 的 mockStatic 功能的方法。但是我正在努力在另一个静态方法中模拟一个静态方法。我确实看到了一些示例,包括 this,但其中 none 实际上帮助了我,或者我不了解实际功能? (我一无所知)
例如。我有一个 class 如下,完整的代码是 here。
public static byte[] encrypt(File file, byte[] publicKey, boolean verify) throws Exception {
//some logic here
PGPPublicKey encryptionKey = OpenPgpUtility.readPublicKey(new ByteArrayInputStream(publicKey));
//some other logic here
}
public/private static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException {
//Impl of this method is here
}
我的测试用例是:
@Test
public void testEncrypt() throws Exception {
File mockFile = Mockito.mock(File.class);
byte[] publicKey = { 'Z', 'G', 'V', 'j', 'b', '2', 'R', 'l', 'Z', 'F', 'B', 'L', 'Z', 'X', 'k', '=' };
boolean flag = false;
PGPPublicKey mockPGPPublicKey = Mockito.mock(PGPPublicKey.class);
InputStream mockInputStream = Mockito.mock(InputStream.class);
PowerMockito.mockStatic(OpenPgpUtility.class);
PowerMockito.when(OpenPgpUtility.readPublicKey(mockInputStream)).thenReturn(mockPGPPublicKey);
System.out.println("Hashcode for PGPPublicKey: " + OpenPgpUtility.readPublicKey(mockInputStream));
System.out.println("Hashcode for Encrypt: " + OpenPgpUtility.encrypt(mockFile, publicKey, flag));
}
当我调用 OpenPgpUtility.encrypt(mockFile, publicKey, flag)
时,这个方法实际上并没有被调用。
如何在 encrypt(...)
中模拟 readPublicKey(...)
方法的结果?
我在 SOF in somebody's post 中找到了解决方案。
在我的例子中,我使用了与下面相同的 PowerMockito 部分模拟。
PowerMockito.stub(PowerMockito.method(OpenPgpUtility.class, "readPublicKey", InputStream.class)).toReturn(mockPGPPublicKey);
这让我可以模拟 readPublicKey()
但实际上是对 encrypt()
的调用
我已经编写了测试用例来模拟静态 classes 和使用 PowerMockito 的 mockStatic 功能的方法。但是我正在努力在另一个静态方法中模拟一个静态方法。我确实看到了一些示例,包括 this,但其中 none 实际上帮助了我,或者我不了解实际功能? (我一无所知)
例如。我有一个 class 如下,完整的代码是 here。
public static byte[] encrypt(File file, byte[] publicKey, boolean verify) throws Exception {
//some logic here
PGPPublicKey encryptionKey = OpenPgpUtility.readPublicKey(new ByteArrayInputStream(publicKey));
//some other logic here
}
public/private static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException {
//Impl of this method is here
}
我的测试用例是:
@Test
public void testEncrypt() throws Exception {
File mockFile = Mockito.mock(File.class);
byte[] publicKey = { 'Z', 'G', 'V', 'j', 'b', '2', 'R', 'l', 'Z', 'F', 'B', 'L', 'Z', 'X', 'k', '=' };
boolean flag = false;
PGPPublicKey mockPGPPublicKey = Mockito.mock(PGPPublicKey.class);
InputStream mockInputStream = Mockito.mock(InputStream.class);
PowerMockito.mockStatic(OpenPgpUtility.class);
PowerMockito.when(OpenPgpUtility.readPublicKey(mockInputStream)).thenReturn(mockPGPPublicKey);
System.out.println("Hashcode for PGPPublicKey: " + OpenPgpUtility.readPublicKey(mockInputStream));
System.out.println("Hashcode for Encrypt: " + OpenPgpUtility.encrypt(mockFile, publicKey, flag));
}
当我调用 OpenPgpUtility.encrypt(mockFile, publicKey, flag)
时,这个方法实际上并没有被调用。
如何在 encrypt(...)
中模拟 readPublicKey(...)
方法的结果?
我在 SOF in somebody's post 中找到了解决方案。
在我的例子中,我使用了与下面相同的 PowerMockito 部分模拟。
PowerMockito.stub(PowerMockito.method(OpenPgpUtility.class, "readPublicKey", InputStream.class)).toReturn(mockPGPPublicKey);
这让我可以模拟 readPublicKey()
但实际上是对 encrypt()