Spring boot runs junit and reports an error

Copy an instance of spring boot junit online and want to test the Request request

 

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

 

After replacing the url with an available address, run as -> junit test found an error, the log is as follows:

 

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).

 

The error message means that the database is not configured properly, and the database type is NONE. But in fact, my spring boot can be started, and the controller can be accessed through the browser to get the correct data. So it is inferred that there must be a problem with the spring boot junit configuration.

 

Then I searched again and found

 

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

 

 

@RunWith(SpringJUnit4ClassRunner.class)  
@SpringBootTest(classes=Application.class)// Specify the startup class of spring-boot   
//@SpringApplicationConfiguration(classes = Application.class)// 1.4.0 前版本  
public class SpringBootJdbcTest {  
       

 

 

I checked that my springboot version is 1.5.3. And the configuration in junit is

 

 

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

 

 

Decisively replace SpringApplicationConfiguration and WebAppConfiguration with SpringBootTest

Run again, no more errors are reported, and the test passes.

 

After further investigation, the reason was found

 @SpringBootTest will configure the spring boot environment and @ContextConfiguration will only configure the spring environment.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326472826&siteId=291194637