org.powermock.reflect.internal.WhiteboxImpl对方法java.lang.Object.clone()的非法反射访问

时间:2019-10-17 22:53:22

标签: java spring spring-boot mocking powermock

我想使用此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

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

1 个答案:

答案 0 :(得分:0)

已经有一些疑问:

what is an illegal reflective access

JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState

还有一些。

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

WorkbooksOpen Filename = "C:\Users\Aqu19\Downloads\20191.xlsx"
Dim Name1 As Object
Dim Array1 As Variant
Array1 = ("20192", "20193")
Name "C:\Users\Aqu19\Downloads\20192.xlsx" As "C:\Users\Aqu19\Downloads\" & Array1 & ".xlsx"

End Sub

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

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

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