Нелегальный рефлексивный доступ от org.powermock.reflect.internal.WhiteboxImpl к методу java.lang.Object.clone () - PullRequest
0 голосов
/ 18 октября 2019

Я хочу использовать этот тест 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 голосов
/ 18 октября 2019

Уже есть несколько вопросов по этому поводу:

что такое нелегальный рефлексивный доступ

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.

...