org.powermock.reflect.internal.WhiteboxImpl对方法java.lang.Object.clone()的非法反射访问 [英] Illegal reflective access by org.powermock.reflect.internal.WhiteboxImpl to method java.lang.Object.clone()

查看:827
本文介绍了org.powermock.reflect.internal.WhiteboxImpl对方法java.lang.Object.clone()的非法反射访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用此JUnit测试来测试私有方法:

I want to use this JUnit test for testing private method:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ReportingProcessor.class)
public class ReportingTest {

    @Autowired
    ReportingProcessor reportingProcessor;

    @Test
    public void reportingTest() throws Exception {

        ContextRefreshedEvent contextRefreshedEvent = PowerMockito.mock(ContextRefreshedEvent.class);
        Whitebox.invokeMethod(reportingProcessor, "collectEnvironmentData", contextRefreshedEvent);

    }
}

但是我得到了WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.powermock.reflect.internal.WhiteboxImpl (file:/C:/Users/Mainuser/.m2/repository/org/powermock/powermock-reflect/2.0.2/powermock-reflect-2.0.2.jar) to method java.lang.Object.clone() WARNING: Please consider reporting this to the maintainers of org.powermock.reflect.internal.WhiteboxImpl WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release

But I get WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.powermock.reflect.internal.WhiteboxImpl (file:/C:/Users/Mainuser/.m2/repository/org/powermock/powermock-reflect/2.0.2/powermock-reflect-2.0.2.jar) to method java.lang.Object.clone() WARNING: Please consider reporting this to the maintainers of org.powermock.reflect.internal.WhiteboxImpl WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release

有什么办法可以解决这个问题?

Is there some way to fix this?

推荐答案

已经有一些疑问:

什么是非法的反射访问

JDK9:发生了非法的反射访问操作. org.python.core.PySystemState

还有一些.

出于测试目的,您可以从侧面执行反射,而无需使用依赖项.为了更好的理解,我更改了collectEnvironmentData方法的返回类型:

For testing purposes, you can just perform the reflection from your side, with no need to use dependencies. I have changed the return type of the collectEnvironmentData method just for a better understanding:

 @EventListener
 private String collectEnvironmentData(ContextRefreshedEvent event) {
    return "Test";
 }

您可以通过以下方式获得其伪装结果:

You can achieve the pretended result by accessing it this way:

    @Test
    public void reportingTest() throws Exception {

        ContextRefreshedEvent contextRefreshedEvent = PowerMockito.mock(ContextRefreshedEvent.class);

        Method privateMethod = ReportingProcessor.class.
                getDeclaredMethod("collectEnvironmentData", ContextRefreshedEvent.class);

        privateMethod.setAccessible(true);

        String returnValue = (String)
                privateMethod.invoke(reportingProcessor, contextRefreshedEvent);
        Assert.assertEquals("Test", returnValue);
    }

使用JDK13在我的控制台上没有警告.

No warnings on my console, with JDK13.

这篇关于org.powermock.reflect.internal.WhiteboxImpl对方法java.lang.Object.clone()的非法反射访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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