使用@EnableAutoConfiguration在Spring Boot中配置JUnit测试

时间:2015-04-10 02:38:32

标签: spring-boot spring-test

我的服务器就像这样

@ComponentScan
@EnableAutoConfiguration
public class Server {
    public static void main(String[] args) {
        SpringApplication server = new SpringApplication(Server.class);
        server.setShowBanner(false);
        server.run(args);
    }
}

我的测试配置就像这样

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Server.class)
public class MyTest {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Test
    public void test1(){
        int countRow = jdbcTemplate.queryForObject("select count(*) from basic_auto_brand;",Integer.class);
    }

我将收到错误:

10:35:28.719 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6fd02e5] to prepare test instance [com.nwche.test.MyTest@5700d6b1]
java.lang.IllegalArgumentException: Can not build an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.

0 个答案:

没有答案