Java Spring:单元测试失败,出现IllegalStateException

时间:2016-12-02 15:00:17

标签: java spring unit-testing spring-mvc

使用Java Spring应用程序,并创建了MLController类和相应的@Bean。我以与每个其他方式相同的方式声明了Bean,但是单元测试contextLoads()阻止了应用程序的构建。

我删除了班级中除了最简单的功能之外的一切,但它仍然失败了!我知道这个Bean导致了问题,因为它在我删除它时构建。可能是什么问题?

错误

live.cibc.labs.springbackend.SpringbackendApplicationTests > contextLoads FAILED
    java.lang.IllegalStateException
        Caused by: org.springframework.beans.factory.BeanCreationException
            Caused by: java.lang.IllegalStateException
1 test completed, 1 failed
:test FAILED
FAILURE: Build failed with an exception.

MLController类

@RestController
public class MLController {

    MLService mlService;

    public void setMlService(MLService service) {
        this.mlService = service;
    }

    @RequestMapping("/testindex")
    public String index() {
        return String.valueOf("BRUH");
    }
}

Application.java

@SuppressWarnings("WeakerAccess")
@SpringBootApplication

public class SpringbackendApplication {

    @Autowired
    Environment env;

    public static void main(String[] args) {
        SpringApplication.run(SpringbackendApplication.class, args);
        FirebaseUtil.initializeFirebase();
    }

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName("org.postgresql.Driver");

        if (System.getenv("ENVIRONMENT") != null && System.getenv("ENVIRONMENT").equals("production")) {
            dataSource.setUrl("jdbc:postgresql://" + this.env.getProperty("POSTGRES_PROD_URL"));
        } else {
            dataSource.setUrl("jdbc:postgresql://" + this.env.getProperty("POSTGRES_DEV_URL"));
        }

        dataSource.setUsername(this.env.getProperty("POSTGRES_USER"));
        dataSource.setPassword(this.env.getProperty("POSTGRES_PASSWORD"));
        return dataSource;
    }

    @Bean
    public UserController userController() {
        UserController userController = new UserController(getUserDAO(), getYodleeDAO());
        userController.setCobrandSession(cobrandSession());
        userController.setUserSessionManager(userSessionManager());
        userController.setAccountsService(accountsService());
        userController.setTransactionsService(transactionsService());
        return userController;
    }

    @Bean
    public TestController testController() {
        TestController testController = new TestController();
        testController.setCobrandSession(cobrandSession());
        testController.setUserSessionManager(userSessionManager());
        testController.setAccountsService(accountsService());
        testController.setTransactionsService(transactionsService());
        return testController;
    }

    @Bean
    public AccountsService accountsService() {
        AccountsService accountsService = new AccountsService();
        accountsService.setApiBase(this.env.getProperty("YODLEE_API_BASE"));
        accountsService.setRestTemplate(restTemplate());
        accountsService.setGson(gson());
        return accountsService;
    }

    @Bean
    public TransactionsService transactionsService() {
        TransactionsService transactionsService = new TransactionsService();
        transactionsService.setRestTemplate(restTemplate());
        transactionsService.setGson(gson());
        return transactionsService;
    }

    @Bean
    public MLService mlService() {
        MLService service = new MLService();
        service.setRestTemplate(restTemplate());
        return service;
    }

    @Bean
    public MLController mlController() {
        MLController controller = new MLController();
        controller.setMlService(new MLService());
        return controller;
    }

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setOutputStreaming(false); // If we don't turn this off, we may get HttpRetryException on 401's.
        return new RestTemplate(factory);
    }

    @Bean
    public Gson gson() {
        return new Gson();
    }
}

0 个答案:

没有答案