我们可以用 PowerMock 创建一个 java.lang.Class 的模拟实例吗? [英] Can we create a mocked instance of java.lang.Class with PowerMock?

查看:61
本文介绍了我们可以用 PowerMock 创建一个 java.lang.Class 的模拟实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个模拟 java.lang.Class 类实例的测试.这可以通过 PowerMock 实现吗?

I need to write a test that mocks a instance of the java.lang.Class class. Is this possible via PowerMock?

我尝试执行以下操作:

PowerMock.createMock(Class.class);

结果是:

java.lang.IllegalAccessError: java.lang.Class
    at sun.reflect.GeneratedSerializationConstructorAccessor12.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)
    at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:223)
    at org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139)
    at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2146)
    at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:89)

根据 PowerMock 的文档,这应该是可能的,但我仍然收到此错误.

According to the documentation of PowerMock this should be possible but still I get this error.

有人设法做到了吗?

为什么我需要这个?在测试的编码中有以下语句:

Why do I need this? In the tested coding there is following statement:

if (someObject.getClass().getName().equals(SOME_CLASS_NAME_THAT_I_DONT_HAVE_ACCESS_TO)) { ... do some stuff ... }

我需要我的测试来达到if"中的编码,我什至不能提供具有相应名称的类的模拟实例.

I need my test to reach the coding inside the "if" and I CANNOT provide even a mocked instance of the class that has the corresponding name.

作为一种解决方法,我可以在测试中创建一个具有相同名称和包的类,但它很难看.

As a workaround I can just create a class with the same name and package in the tests but it is ugly.

我也尝试了 此链接

import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.junit.Test;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Test1.class})
public class Test1 {

    @Test
    public void test() {
        PowerMock.createMock(Class.class);
    }

}

结果还是一样:"java.lang.IllegalAccessError: java.lang.Class"

And the result is the same: "java.lang.IllegalAccessError: java.lang.Class"

所以作为最终结果 - 似乎没有办法创建 java.lang.Class 的模拟实例

So as a final result - it seems that there is no way to create a mocked instance of java.lang.Class

谢谢

推荐答案

根据此声明,

...

在 java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

...

PowerMock(使用 Objenesis 库)尝试实例化 java.lang.Class,它只能由 JVM 实例化.来自 docs:

PowerMock (using Objenesis library) tries to instantiate java.lang.Class, which could be instantiated only by JVM. From docs:

类没有公共构造函数.相反,Java 虚拟机在加载类时以及通过调用类加载器中的 defineClass 方法时自动构造类对象.

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

不久,我几乎可以肯定不可能手动创建 java.lang.Class 的实例.如果我错了,请纠正我.

Shortly, I'm almost sure that it's not possible to make instance of java.lang.Class manually. Please correct me if I wrong.

顺便说一句,

不幸的是,我需要更改 getClass().getName() 的返回值

Unfortunately I need to change the return value of the getClass().getName()

getClass() 方法的嘲讽不是你的选择吗?

Isn't mocking of getClass() method an option for you?

这篇关于我们可以用 PowerMock 创建一个 java.lang.Class 的模拟实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆