gpt4 book ai didi

android - 使用 power mockito 模拟方法调用 - org.powermock.api.mockito.ClassNotPreparedException

转载 作者:IT王子 更新时间:2023-10-28 23:32:03 38 4
gpt4 key购买 nike

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

 Base64.encodeToString(byteArray, Base64.DEFAULT);

为了模拟这个,我使用 mockStatic 方法如下,带有@PrepareForTest 注释。

 PowerMockito.mockStatic(Base64.class);

但 Android Studio 正在返回我仍然返回如下错误。

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();
imageBitmap.compress(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());
}

}

Gradle 依赖项

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'

最佳答案

简短的回答你不能。这里来自 FAQ :

What are the limitations of 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 sobe vigilant.

有关此限制的更多信息:

我可以模拟静态方法吗?

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

如果你想使用 PowerMock 试试这样:

@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 it's now possible to mock final Class and final Method .这是一个可选的选项。您需要使用以下内容创建文件 src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker:

mock-maker-inline

编辑 2:由于 Mockito 3.4.0 现在可以 mock static method太:

try (MockedStatic mocked = mockStatic(Base64.class)) {
mocked.when(() -> Base64.encodeToString(eq(array), eq(Base64.DEFAULT))).thenReturn("bar");
assertEquals("bar", Base64.encodeToString(array, Base64.DEFAULT));
mocked.verify(() -> Base64.encodeToString(any(), anyIn());
}

此外,您可以直接添加为依赖项 org.mockito:mockito-inline:+ 并避免手动创建 or.mockito.plugins.MockMaker 文件

从 Mockito 3.5.0 开始,您还可以 mock object construction .

关于android - 使用 power mockito 模拟方法调用 - org.powermock.api.mockito.ClassNotPreparedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39405870/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com