由于数据库相关错误,主应用程序和测试无法运行

问题描述 投票:0回答:1

我正在尝试为我的 Spring Boot 应用程序创建测试(我以前从未创建过测试,所以这是我第一次。)。我在主应用程序中使用 MongoDB Atlas,并在测试中使用 H2 数据库。我在

application.properties
目录中创建了另一个
test
文件用于测试。在我为测试创建
application.properties
文件并运行测试后,它尝试连接我的 MongoDB 数据库。为了防止这种情况,我将此行添加到我的测试
application.properties
文件中:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
.

但是,这一行导致了这个错误:

Parameter 0 of constructor in com.aozbek.form.service.UserDetailsServiceImp required a bean named 'mongoTemplate' that could not be found.
我不知道为什么会发生这种情况,因为在我的测试中我没有使用与
UserDetailsServiceImp
文件相关的任何内容。这是我的测试文件:

@SpringBootTest
@AutoConfigureTestDatabase
class FieldRepositoryTest {

    @Autowired
    private FieldRepository underTest;

    @Test
    void itShouldGetFormFieldById() {

        //given
        FormField formField = new FormField(
                "1",
                "School",
                "text",
                "2"
        );
        underTest.save(formField);

        //when
        Optional<FormField> searchedFormField = underTest.getFormFieldById("1");

        //then
        assertThat(searchedFormField).isPresent();
        assertThat(searchedFormField).contains(formField);

    }
}

这是我在测试目录中的

application.properties
文件:

spring.datasource.url=jdbc:h2://mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration

此外,在添加这些

application.properties
FieldRepositoryTest.java
文件之前,我的主应用程序运行良好。但是,在添加所有这些之后,我尝试运行我的主应用程序以查看它是否有效,但我收到了此错误:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

这是我的主应用程序的

application.properties
文件:

spring.data.mongodb.database=form-data-db
spring.data.mongodb.uri=mongodb+srv://information-to-access-db
server.error.include-binding-errors=always

我不明白为什么这两个

application.properties
文件无法正常协同工作。另外,为什么我需要
'mongoTemplate'
bean 来构造
UserDetailsServiceImp
来进行上述测试。因为我在测试中没有使用任何与之相关的东西。在我的测试中,我直接使用
FieldRepository
,而不需要
UserDetailsServiceImp
。所以,我有几个问题想问你,如果你能回答,我会很高兴。

1- 我怎样才能使这个测试正常工作?

2- 为什么这些

application.properties
文件无法正常协同工作?

3- 为什么我的测试中需要

'mongoTemplate'
bean:
UserDetailsServiceImp
? (我问这个是因为我的测试根本不使用这个类。)

编辑:

这是我的

FieldRepository
实现:

@Repository
public interface FieldRepository extends MongoRepository<FormField, String> {
    Optional<FormField> getFormFieldById(String id);
    List<FormField> findAllByFormId(String id);
}
java spring-boot junit5
1个回答
0
投票

你不能使用H2代替MongoDB,H2是一个带有表等的关系数据库,而MongoDB是一个基于文档的数据库。

Spring 有一个用于 MongoDB 的库 spring-data-mongodb 和另一个用于关系数据库的库 spring-data-jpa。

您的代码中的

MongoTemplate
FieldRepository
有过渡依赖,因为该接口扩展了
MongoRepository
(参见它的 JavaDocs)。类
SimpleMongoRepository
在其构造函数中需要一个
MongoOperations
的实例,其实现是
MongoTemplate

如果你想进行集成测试,你将必须使用 MongoDB - 或者你切换到单元测试而不需要真正的数据库交互。

© www.soinside.com 2019 - 2024. All rights reserved.