当前位置: 首页 > 知识库问答 >
问题:

测试MongoDB存储库:创建具有名称的bean时出错:没有符合条件的类型bean

齐雅畅
2023-03-14

运行mvn test在Mongo存储库的单元测试中返回以下错误:

.
├── src/
│   ├── main/
│   │   └── java/
│   │       └── my_package/
│   │           ├── domain/
│   │           │   └── users/
│   │           │       ├── User.java
│   │           │       └── UserRepository.java
│   │           ├── infrastructure/
│   │           │   └── mongo/
│   │           │       └── repositories/
│   │           │           └── UserRepositoryMongoImpl.java
│   │           └── ApplicationConfig.java
│   └── test/
│       └── my_package/
│           └── infrastructure/
│               └── mongo/
│                   └── repositories/
│                       └── UserRepositoryMongoImplTests.java
└── pom.xml
public interface UserRepository {
    public User findByUsername(String username);
    public User save(User user);
    public void delete(User user);
}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("my_package")
public class ApplicationConfig {
}

UserRepositoryMongoImpl.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import my_package.domain.users.User;
import my_package.domain.users.UserRepository;

@Repository
public class UserRepositoryMongoImpl implements UserRepository {
    @Autowired
    private MongoTemplate mongoTemplate;

    private static final String COLLECTION_NAME = "users";

    @Override
    public User findByUsername(String username) {
        return mongoTemplate.findOne(Query.query(Criteria.where("username").is(username)), User.class, COLLECTION_NAME);
    }

    @Override
    public User save(User user) {
        return mongoTemplate.save(user, COLLECTION_NAME);
    }

    @Override
    public void delete(User user) {
        mongoTemplate.remove(user, COLLECTION_NAME);
    }
}

UserRepositoryMongoImplTests.java

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import my_package.domain.users.User;
import my_package.infrastructure.mongo.repositories.users.UserRepositoryMongoImpl;

@Testcontainers
@DataMongoTest(excludeAutoConfiguration = {
    EmbeddedDataSourceConfiguration.class
})
public class UserRepositoryMongoImplTests {
    @Autowired
    private UserRepositoryMongoImpl userRepository;

    @Container
    private MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.4.4"));

    @BeforeEach
    void mongoDbContainerStarting() {
        mongoDBContainer.start();
        System.setProperty("spring.data.mongodb.uri", mongoDBContainer.getReplicaSetUrl());
    }

    @AfterEach
    void mongoDbContainerStoping() {
        mongoDBContainer.stop();
    }

    @Test
    public void shouldSaveValidNewUser() {
        User user = new User("username123", "heuehueheuh");
        user = userRepository.save(user);

        assert(user.id() != null);
        assert(user.username().equals("username123"));
        assert(user.password().equals("heuehueheuh"));
    }
}

最后一个:pom.xml

<?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.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>br.nom.carneiro.carlos</groupId>
    <artifactId>future_software_003-ws</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-project</name>
    <description>Random description</description>
    <properties>
        <java.version>11</java.version>
        <testcontainers.version>1.15.2</testcontainers.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>mongodb</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-guava</artifactId>
            <version>3.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.testcontainers</groupId>
                <artifactId>testcontainers-bom</artifactId>
                <version>${testcontainers.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.8</version>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html</backend>
                            <doctype>book</doctype>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.restdocs</groupId>
                        <artifactId>spring-restdocs-asciidoctor</artifactId>
                        <version>${spring-restdocs.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
    null
    @Bean
    public UserRepositoryMongoImpl userRepositoryMongoImpl() {
        return new UserRepositoryMongoImpl();
    }

共有1个答案

方苗宣
2023-03-14

我在@SpringBootApplication类中添加了以下注释,从而解决了问题:@ComponentScan(basePackages={“My_Package.Infrastructure.Mongo”})。结果我的ComponentScan注释是错误的,并且在错误的类中。

我通过阅读问题NoSuchBeanDefinitionException的答案得出了这个结论:没有限定类型的bean

 类似资料:
  • 还有这个 UnsatisfiedDependencyException:创建名为“Client Service”的bean时出错:通过字段“Client Repository”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionexception:没有“com.kopylov.repository.clien

  • 我正在用Spring Boot实现Rest API。因为我的实体类来自另一个包,所以我必须用注释来指定它。另外,我使用来指定定义JPA存储库的包。下面是我的项目的样子: 在我的controller类中,我有一个SeqService对象Autowired。 是一个接口,我从它为创建了一个Bean类。在中,我对JPA存储库进行了描述: 我不明白这个错误。和排位赛bean有什么关系?

  • 我目前正在做一个小项目,计划展示数据库中的一些文档。我遇到了这个问题,springboot将我的控制器称为bean。这是我真正的豆子。 和我的控制器: 我的存储库 错误: 组织。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“productController”的bean时出错,该bean在文件[E:\IntelijProj\target

  • 我已经浏览了http://docs.spring.io/spring-data/redis/docs/current/reference/html/,对我来说没有什么新内容。我想知道我错过了什么,我将感谢任何投入。 我使用Spring Data Redis 1.7.2.Release,Spring Boot 1.3.6.Release

  • 整个文件是基于我在Spring Initializr中选择的内容生成的。我一直得到的错误是: 如果你需要更多的细节,尽管问。我会尽力送去的。