vendredi 1 janvier 2016

Test failes when defined scanBasePackages for @SpringBootApplication

I am trying to write a JavaFX aplication integrated with Spring Boot. It works, but there is some problem with integration test. I want to place MyApplication and Config classes to different packages, so, I add in MyApplication

scanBasePackages = "com.saranaisoft"

and then my test failes with exceptions:

java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainView' defined in class path resource [com/saranaisoft/core/Config.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.saranaisoft.core.View]: Factory method 'getMainView' threw exception; nested exception is java.lang.ExceptionInInitializerError Caused by: java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalStateException: Toolkit not initialized

if I remove scanBasePackages = "com.saranaisoft"

@Lazy
@SpringBootApplication()
public class MyApplication extends Application {

Test become succesfull, but application itself failes with exception about "cannot find beans" defined in Config.

my code:

project folders

MyApplication.java

@Lazy
@SpringBootApplication(scanBasePackages = "com.saranaisoft")
public class MyApplication extends Application {


    private static String[] savedArgs;

    protected ConfigurableApplicationContext context;

    @Override
    public void init() throws Exception {
        context = SpringApplication.run(getClass(), savedArgs);
        context.getAutowireCapableBeanFactory().autowireBean(this);
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        context.close();
    }

    protected static void launchApp(Class<? extends Application> clazz, String[] args) {
        savedArgs = args;
        Application.launch(clazz, args);
    }


    @Value("${ui.title:JavaFX приложение}")
    private String windowTitle;

    @Qualifier("mainView")
    @Autowired
    private View view;

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle(windowTitle);
        stage.setScene(new Scene(view.getRoot()));
        stage.setResizable(true);
        stage.centerOnScreen();
        stage.show();
    }

    public static void main(String[] args) {
        launchApp(MyApplication.class, args);
    }

}

Config.java

@Configuration
public class Config {

    @Bean(name = "mainView")
    public View getMainView() throws IOException {
        return loadView("fxml/main.fxml");
    }

    @Bean
    public Controller getMainController() throws IOException {
        return (Controller) getMainView().getController();
    }

    protected View loadView(String url) throws IOException {
        InputStream fxmlStream = null;
        try {
            fxmlStream = getClass().getClassLoader().getResourceAsStream(url);
            FXMLLoader loader = new FXMLLoader();
            loader.load(fxmlStream);
            return new View(loader.getRoot(), loader.getController());
        } finally {
            if (fxmlStream != null) {
                fxmlStream.close();
            }
        }
    }

}

MyApplicationTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
public class MyApplicationTests {

    @Test
    public void contextLoads() {
        System.out.println("lol");
    }

}

Aucun commentaire:

Enregistrer un commentaire