![](/img/trans.png)
[英]tried to access method java.lang.Object.clone()Ljava/lang/Object; from class ar.com.fdvs.dj.util.Utils
[英]Illegal reflective access by org.powermock.reflect.internal.WhiteboxImpl to method java.lang.Object.clone()
我想用这个 JUnit 测试来测试私有方法:
@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
有没有办法解决这个问题?
已经有几个问题:
JDK9:发生了非法反射访问操作。 org.python.core.PySystemState
还有一些。
出于测试目的,您可以只从您身边执行反射,而无需使用依赖项。 为了更好地理解,我更改了 collectEnvironmentData 方法的返回类型:
@EventListener
private String collectEnvironmentData(ContextRefreshedEvent event) {
return "Test";
}
您可以通过这种方式访问它来实现假装的结果:
@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 在我的控制台上没有警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.