使用Power Mockito模拟方法调用

编程入门 行业动态 更新时间:2024-10-25 05:24:44
本文介绍了使用Power Mockito模拟方法调用-org.powermock.api.mockito.ClassNotPreparedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个图像加载器类,我需要在其中测试一些静态方法.由于Mockito不支持静态方法,因此我切换到Power Mockito.但是我正在测试的静态方法有一个方法调用

I have an image loader class and i need to test some static methods in it. Since Mockito does not support static methods i switched to Power Mockito. But the static method i am testing has a method call

Base64.encodeToString(byteArray, Base64.DEFAULT);

要对此进行模拟,我将使用下面的 mockStatic 方法和@PrepareForTest批注.

To mock this i am using mockStatic method as below with @PrepareForTest annotation.

PowerMockito.mockStatic(Base64.class);

但是Android Studio仍在返回我,但仍然返回以下错误.

But Android studio is returning me still returning me an error as below.

org.powermock.api.mockito.ClassNotPreparedException:该类 android.util.Base64未准备好进行测试.要准备这个课程,请添加 类到"@PrepareForTest"注释.

org.powermock.api.mockito.ClassNotPreparedException: The class android.util.Base64 not prepared for test. To prepare this class, add class to the '@PrepareForTest' annotation.

下面是我的完整代码.

要测试的代码:

import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Base64; import android.widget.ImageView; public static String convertBitmapToBase64(Bitmap imageBitmap, boolean withCompression) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); imageBitmappress(Bitmap.CompressFormat.PNG, 120, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); return Base64.encodeToString(byteArray, Base64.DEFAULT); }

测试类代码

import android.graphics.Bitmap; import android.util.Base64; import org.junit.Before; import org.junit.runner.RunWith; import org.mockito.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.testng.annotations.Test; @RunWith(PowerMockRunner.class) @PrepareForTest({Base64.class}) public class ImageLoaderTest { @Test public void testConvertBitmap(){ byte[] array = new byte[20]; PowerMockito.mockStatic(Base64.class); PowerMockito.when(Base64.encodeToString(array, Base64.DEFAULT)).thenReturn("asdfghjkl"); Bitmap mockedBitmap= PowerMockito.mock(Bitmap.class); String output = ImageLoaderUtils.convertBitmapToBase64(mockedBitmap); assert (!output.isEmpty()); }

}

等级依赖性

testCompile 'junit:junit:4.12' testCompile 'org.powermock:powermock:1.6.5' testCompile 'org.powermock:powermock-module-junit4:1.6.5' testCompile 'org.powermock:powermock-api-mockito:1.6.5'

推荐答案

您无法回答的简短内容.来自常见问题解答:

Short answer you can't. Here from the FAQ:

Mockito有哪些局限性

What are the limitations of Mockito

  • 无法模拟最终课程
  • 无法模拟静态方法
  • 无法模拟最终方法-执行它们的实际行为没有任何异常. Mockito无法警告您有关模拟最终方法的信息,因此 保持警惕.
  • Cannot mock final classes
  • Cannot mock static methods
  • Cannot mock final methods - their real behavior is executed without any exception. Mockito cannot warn you about mocking final methods so be vigilant.

有关此限制的更多信息:

Further information about this limitation:

我可以模拟静态方法吗?

Can I mock static methods?

不. Mockito更喜欢面向对象和依赖项注入 静态,难以理解的程序代码改变.如果你 处理可怕的旧代码,您可以使用JMockit或Powermock进行模拟 静态方法.

No. Mockito prefers object orientation and dependency injection over static, procedural code that is hard to understand & change. If you deal with scary legacy code you can use JMockit or Powermock to mock static methods.

如果要使用PowerMock,请尝试如下操作:

If you want to use PowerMock try like this:

@RunWith(PowerMockRunner.class) @PrepareForTest( { Base64.class }) public class YourTestCase { @Test public void testStatic() { mockStatic(Base64.class); when(Base64.encodeToString(argument)).thenReturn("expected result"); } }

在 Mockito 2中,现在可以模拟最终的类了和最终方法.这是一个选择加入的选项.您需要使用以下内容创建文件src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker:

In Mockito 2 it's now possible to mock final Class and final Method. It's an opt-in option. You need to create the file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker with the following content:

mock-maker-inline

更多推荐

使用Power Mockito模拟方法调用

本文发布于:2023-10-31 06:35:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Power   Mockito

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!