`
guoweisharp
  • 浏览: 4518 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring boot 跑junit 报错

 
阅读更多

在网上copy一个spring boot junit的实例,想要测试一下Request请求

 

http://blog.csdn.net/catoop/article/details/50752964

 

将url替换成可用地址后, run as ->  junit test 发现报错了,log如下:

 

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

 

报错的意思是database没有配置好,database type为NONE。但实际上我的spring boot是可以启动,通过浏览器也是可以访问controller获取正确数据的。所以推断肯定是spring boot junit配置有问题。

 

然后又找了下资料发现

 

http://blog.csdn.net/u014695188/article/details/52262895

 

 

@RunWith(SpringJUnit4ClassRunner.class)  
@SpringBootTest(classes=Application.class)// 指定spring-boot的启动类   
//@SpringApplicationConfiguration(classes = Application.class)// 1.4.0 前版本  
public class SpringBootJdbcTest {  
       

 

 

检查了下我的springboot版本是1.5.3。而junit 里的配置为

 

 

写道
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Application.class)
public class ControllerTest {

 

 

果断将SpringApplicationConfiguration 和WebAppConfiguration 替换为SpringBootTest

再跑,不再报错,测试通过。

 

再进一步查了下原因发现

 @SpringBootTest 会配置spring boot环境而@ContextConfiguration 只会配置spring环境。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics