【问题标题】:Spring Boot Database initialization failed: SimpleDriverDataSource could not initialize classSpring Boot 数据库初始化失败:SimpleDriverDataSource 无法初始化类
【发布时间】:2022-01-18 13:34:42
【问题描述】:

我正在尝试使用 Spring Boot 上下文运行测试,但出现错误“无法初始化类 org.springframework.jdbc.datasource.SimpleDriverDataSource”。

我已经在这个问题上苦苦挣扎了几天,但无法正常工作。可能我对 Spring Boot 测试不够熟悉,但在 Spring Boot 2.3.4 版上一切正常。升级到 2.5.7 及更高版本后,(2.6.1) 问题仍然存在。

测试类:

@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@ContextConfiguration(classes = TestApplication.class)
@SpringBootTest(properties = {
        "spring.sql.init.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE",
        "spring.sql.init.driver-class-name=org.h2.Driver",
        "spring.sql.init.username=user",
        "spring.sql.init.password=pass",
        "spring.jpa.hibernate.ddl-auto=update",
        "spring.jpa.show-sql=true"
}
)
public class UserServiceTest {

    private static final String USERNAME_COLUMN = "username";

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;

    @BeforeEach
    @AfterEach
    public void cleanDatabase() {
        userRepository.deleteAll();
        roleRepository.deleteAll();
    }

    @Test
    @Transactional
    public void shouldRegisterUser() {
        roleRepository.saveAndFlush(RoleFixture.createRoleEntity(UserType.USER.name()));

        final String username = "username";
        final UserType type = UserType.USER;

        final UserRegister userRegister = UserFixture.createUserRegister(
                username,
                "password",
                "email@test.com",
                25,
                "name",
                "last_name",
                type
        );
        final UserResponse userResponse = userService.registerUser(userRegister);

        assertThat(userResponse).isNotNull();
        assertThat(userResponse.getUserId()).isNotNull();
        assertThat(userResponse.getUsername()).isEqualTo(username);
        assertThat(userResponse.getType()).isEqualTo(type);

        assertThat(userRepository.findByUserId(userResponse.getUserId())).isNotEmpty();
    }

上下文初始化器类:

package hr.fitbit.demo.fitbitconnect.testsupport;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "hr.fitbit.demo.fitbitconnect")
public class TestApplication {
}

Maven 依赖:

 <!-- spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

我尝试包含 spring-boot-data-jpa、h2、spring-boot-jdbc 依赖项,但没有任何帮助。这是项目链接https://github.com/filip194/fitbit-connect 和测试链接:https://github.com/filip194/fitbit-connect/blob/master/subprojects/users/src/test/java/hr/fitbit/demo/fitbitconnect/users/service/UserServiceTest.java

我也曾尝试删除 @AutoConfigureTestDatabase,但后来又出现另一个错误: 无法初始化类 com.zaxxer.hikari.HikariDataSource,即使我在 pom 中明确包含依赖项,我也找不到解决方案。

我目前对此感到困惑,无法继续我的教育项目。任何帮助,将不胜感激。 谢谢!

编辑 1: 使用 spring.datasource.spring.sql.init. 属性不会同时更改测试结果或错误消息案例。

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa spring-data


    【解决方案1】:

    我建议你做出以下决定:

    1. 添加@EnableJpaRepositories@EntityScan
       @EnableJpaRepositories(basePackageClasses = {UserRepository.class,  RoleRepository.class})
       //@EnableJpaRepositories(basePackages = "hr.fitbit.demo.fitbitconnect.dao.repository")
       @EntityScan(basePackageClasses = {UserEntity.class, RoleEntity.class})
       @SpringBootTest(classes = TestApplication.class)
       public class UserServiceTest {
    
    1. 在模块dao 中添加带有@SpringBootApplication 的类到文件夹dao/src/test/java
       package hr.fitbit.demo.fitbitconnect.dao;
    
       import org.springframework.boot.autoconfigure.SpringBootApplication;
    
       @SpringBootApplication
       public class MockTestApplication {
       }
    

        @DataJpaTest
        public class RoleEntityTest {
    

    更多信息在这里https://www.baeldung.com/spring-boot-unable-to-find-springbootconfiguration-with-datajpatest

    1. @EnableJpaRepositories@EntityScan 添加到TestSupport 类和FitbitServiceTest 类。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-12
      • 2018-02-14
      • 2018-03-22
      • 1970-01-01
      • 2017-07-29
      • 2017-09-14
      • 2018-06-10
      • 2019-01-22
      相关资源
      最近更新 更多