I want to create a JUnit test for this private method:
@Component
public class ReportingProcessor {
@EventListener
private void collectEnvironmentData(ContextRefreshedEvent event) {
}
}
I tried this:
@SpringBootApplication
public class ReportingTest {
@Bean
ServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory();
}
@Test
public void reportingTest() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
GenericApplicationContext parent = new GenericApplicationContext();
parent.refresh();
ConfigurableApplicationContext context = new SpringApplicationBuilder(Configuration.class).parent(parent).run();
ContextRefreshedEvent refreshEvent = new ContextRefreshedEvent(context);
ReportingProcessor instance = new ReportingProcessor();
Method m = ReportingProcessor.class.getDeclaredMethod("collectEnvironmentData", ContextRefreshedEvent.class);
m.setAccessible(true);
m.invoke(instance, refreshEvent);
}
}
But I get exception: Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
What is the proper way to implement a mocked object for ContextRefreshedEvent?