为@SpringBootApplication

时间:2016-01-01 14:09:46

标签: spring testing javafx spring-boot

我正在尝试编写与Spring Boot集成的JavaFX应用程序。它有效,但集成测试存在一些问题。我想将MyApplication和Config类放到不同的包中,因此,我添加了MyApplication

scanBasePackages = "com.stackoveflow"

然后我的测试失败了例外:

  

java.lang.IllegalStateException:无法加载ApplicationContext   引起:org.springframework.beans.factory.BeanCreationException:   创建名称为' mainView'的bean时出错在类路径中定义   resource [com / stackoveflow / core / Config.class]:Bean实例化via   工厂方法失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例化[com.stackoveflow.core.View]:工厂方法' getMainView'   抛出异常;嵌套异常是   java.lang.ExceptionInInitializerError由以下原因引起:   java.lang.ExceptionInInitializerError由以下原因引起:   java.lang.IllegalStateException:Toolkit未初始化

如果我删除scanBasePackages = "com.stackoveflow"

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

测试变得成功,但是应用程序本身就会出现异常情况,因为"无法找到bean"在Config。中定义。

我的代码:

project folders

MyApplication.java

@Lazy
@SpringBootApplication(scanBasePackages = "com.stackoveflow")
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");
    }

}

0 个答案:

没有答案