简单Springboot JUnitTest失败,未找到支持的数据源类型

1tu0hz3e  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(188)

我对Sprinboot应用程序进行了一个非常简单的JUnit测试:
@SpringBootTest(类={应用程序.类})公共类测试上下文{
@测试void contextLoads(应用程序上下文上下文){AssertNotNull(上下文);()}}
一个非常简单的应用程序使用两个数据库:

@SpringBootApplication
@EnableAutoConfiguration
public class Application
{
  private static final Logger applogger = LoggerFactory.getLogger("Application"); 

  @Autowired
  private DbSource userSource;
  @Autowired
  private DbSource dataSource;

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

尽管应用程序可以工作,但我无法运行单元测试。返回的错误是:

Failed to load ApplicationContext java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'application': Unsatisfied dependency expressed through field 'userSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSource' defined in class path resource [it/unict/spring/application/configurations/DbSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [it.unict.spring.application.data.DbSource]: Factory method 'getUserSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSource' defined in class path resource [it/unict/spring/application/configurations/DbSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [it.unict.spring.application.data.DbSource]: Factory method 'getUserSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [it.unict.spring.application.data.DbSource]: Factory method 'getUserSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found
Caused by: java.lang.IllegalStateException: No supported DataSource type found

请注意,我将扩展测试,以检查是否存储了正确的DB模式。
更新:
我在单元测试中添加了@ContextConfiguration:

@SpringBootTest
@ContextConfiguration(classes={DbSourceConfig.class, DbSource.class})
public class TestContext
 { 
  @Test
  void contextLoads(ApplicationContext context)
   {
   assertNotNull(context);       
   }
 }

现在的错误是:

Failed to load ApplicationContext java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dbSource': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

POM是:

<?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.1-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>it.unict.spring</groupId>
<artifactId>SpringApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringApplication</name>        
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <start-class>controllers.Application</start-class>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>         
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>   
            <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>                        
        <scope>test</scope>
    </dependency>               

            <dependency>
                <groupId>org.mariadb.jdbc</groupId>
                <artifactId> mariadb-java-client</artifactId>
            </dependency>

</dependencies>

<build>
            <finalName>SpringApplication</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

application.properties文件:

spring.datasource.url=jdbc:mariadb://localhost:3306/data
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver

spring.datasource2.url=jdbc:mariadb://localhost:3306/users
spring.datasource2.username=root
spring.datasource2.password=root
spring.datasource2.driver-class-name=org.mariadb.jdbc.Driver

spring.jpa.hibernate.ddl-auto=validate

DbSourceConfig文件:

import it.unict.spring.application.data.DbSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DbSourceConfig
{

 @Value("${spring.datasource.url}")                   
 private String urlUser;
 @Value("${spring.datasource2.url}") 
 private  String urlData;
 @Value("${spring.datasource.username}") 
 private  String username;
 @Value("${spring.datasource.password}") 
 private  String password;    

 @Bean(name="userSource")
 public DbSource getUserSource()
  {
   return new DbSource(urlUser, username, password);
  }

@Bean(name="dataSource")
public DbSource getDataSource()
{
 return new DbSource(urlData, username, password);
}      

}

ServletInitializer档案:

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

@Override
public SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
    return application.sources(Application.class);                
   }
  }
xqk2d5yq

xqk2d5yq1#

junit需要spring-boot-starter-jdbc,但pom.xml中缺少。添加依赖关系解决了此问题

相关问题