Я хочу протестировать класс, который просто завершает работу приложения:
@Component
public class ShutdownManager {
@Autowired
private ApplicationContext applicationcontext;
public int shutdown(int returnCode) {
return SpringApplication.exit(applicationcontext, () -> returnCode);
}
}
Я создал следующий тестовый пример:
public class ShutdownManagerTest {
@Test
public void should_shutdown_gracefully() {
SpringApplication app = new SpringApplication(TestClass.class);
ConfigurableApplicationContext context = app.run();
ShutdownManager shutdownManager = (ShutdownManager)context.getBean("shutdownManager");
int result = shutdownManager.shutdown(10);
assertThat(result).isEqualTo(10);
}
@SpringBootApplication
@ImportResource("classpath:/core/shutdownmanagertest.xml")
private static class TestClass {
public TestClass() {
}
public static void main(String[] args) {
}
}
}
Мой файл shutdownmanagertest.xml содержит только один компонент:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="shutdownManager" class="com.mycodebase.ShutdownManager" />
</beans>
Однако, когда я запускаю это, он жалуется, что:
Field myOtherService in com.mycodebase.MyTask required a bean of type 'com.mycodebase.MyOtherService ' that could not be found.
Класс MyTask находится в том же пакете ShutdownManager, который содержит поле myOtherService, которое имеет @Autowireannocation.Но это не определено в моем тестовом XML, который содержит только один компонент.
Может кто-нибудь помочь мне понять, почему он пытается разрешить бин, который не находится в контексте?