小编Ext*_*me 的帖子

PowerMockito模拟单个静态方法并在另一个静态方法中返回对象

我已经编写了使用PowerMockito的mockStatic功能来模拟静态类和方法的测试用例。但是我正在努力在另一个静态方法中模拟一个静态方法。我确实没有看到包含示例的示例,但是它们都没有真正对我有所帮助,或者我不了解实际功能?(我很无知)

例如。我有一个下面的类,完整的代码在这里

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
}
Run Code Online (Sandbox Code Playgroud)

我的测试用例是:

@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', '=' }; …
Run Code Online (Sandbox Code Playgroud)

java static-methods unit-testing mocking powermockito

4
推荐指数
1
解决办法
2152
查看次数