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

@Spring集成测试中的自动连线和未满足的PendencyException

刘畅
2023-03-14

我有多模块项目与Spring Boot和外部服务器Weblogic.

这些是模块:

>

  • 服务
  • 波姆。xml(dao)。

    这是数据库(存储库、实体)的工作

    <?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
             http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>gov-multiple-modules</artifactId>
            <groupId>gov</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
    
        <groupId>com.dao</groupId>
        <artifactId>dao</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <properties>
            <output.directory.jdbc.oracle>${project.basedir}/src/main/resources</output.directory.jdbc.oracle>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc</artifactId>
                <version>6</version>
                <scope>system</scope>
                <systemPath>${output.directory.jdbc.oracle}/lib/ojdbc6.jar</systemPath>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <argLine>-Dfile.encoding=UTF8</argLine>
                    </configuration>
                </plugin>
    
            </plugins>
        </build>
    
    </project>
    
    
    • 波姆。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
             http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>gov-multiple-modules</artifactId>
            <groupId>gov</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.service</groupId>
        <artifactId>service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>com.dao</groupId>
                <artifactId>dao</artifactId>
                <version>${version.dao.module}</version>
            </dependency>
    
    
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct</artifactId>
                <version>${version.mapstruct}</version>
            </dependency>
    
        </dependencies>
    
    
        <build>
              <plugins>
    
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <configuration>
                          <argLine>-Dfile.encoding=UTF8</argLine>
                      </configuration>
                  </plugin>
    
                <plugin> 
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${version.apache.maven.plugins}</version>
                    <groupId>org.apache.maven.plugins</groupId>
    
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>${version.mapstruct}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    
    

    >

    它是处理来自客户端(Contoroller和restcontroller)的请求的工作。

    应用程序中有一个切入点。

    <?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
             http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>gov-multiple-modules</artifactId>
            <groupId>gov</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.web</groupId>
        <artifactId>web</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>com.service</groupId>
                <artifactId>service</artifactId>
                <version>${version.service.module}</version>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>weblogic-war-gov</finalName>
            <plugins>
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <argLine>-Dfile.encoding=UTF8</argLine>
                    </configuration>
                </plugin>
    
    
                <plugin> <!--It is for convert beans-->
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${version.apache.maven.plugins}</version>
                    <groupId>org.apache.maven.plugins</groupId>
    
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>${version.mapstruct}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
    
            </plugins>
        </build>
    
    </project>
    
    
    • 波姆。xml(parrent)
    <?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>
    
        <modules>
            <module>dao</module>
            <module>service</module>
            <module>web</module>
        </modules>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <groupId>gov</groupId>
        <artifactId>gov-multiple-modules</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <name>gov-multiple-modules</name>
        <description>project with Spring Boot for multiple module applications</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <version.apache.maven.plugins>3.8.1</version.apache.maven.plugins>
            <version.mapstruct>1.3.0.Final</version.mapstruct>
    
            <version.apache.common.lang3>3.9</version.apache.common.lang3>
            <version.apache.commons.text>1.8</version.apache.commons.text>
            <version.apache.commons.beanutils>1.9.4</version.apache.commons.beanutils>
            <version.hibernate.validator>6.0.17.Final</version.hibernate.validator>
            <version.reflection>0.9.11</version.reflection>
            <version.dao.module>0.0.1-SNAPSHOT</version.dao.module>
            <version.service.module>0.0.1-SNAPSHOT</version.service.module>
    
        </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-data-jpa</artifactId>
            </dependency>
    
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!--This artifact need for testing that to find classes into classpath-->
            <dependency>
                <groupId>org.reflections</groupId>
                <artifactId>reflections</artifactId>
                <version>${version.reflection}</version>
                <scope>test</scope>
            </dependency>
    
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${version.apache.common.lang3}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-text</artifactId>
                <version>${version.apache.commons.text}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>${version.apache.commons.beanutils}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-validation</artifactId>
            </dependency>
    
        </dependencies>
    
    </project>
    
    

    进入web模块的是testdir。

    • src/test/java/com/web

    这里是一个仓库

    • src/test/java/com/web/dao/repository/company/CompanyReadRepositoryTest。爪哇
    public interface CompanyReadRepositoryTest extends CrudRepository<Company, Long> {
    
        String nameTable = "company";
        String lastEntryQueryFor =
                "select * from (select t.* from " + nameTable + " t order by 1 desc) where rownum = 1";
    
        @Query(value =lastEntryQueryFor, nativeQuery = true)
        Optional<Company> getLastEntry();
    }
    

    我有一门考试课。

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Sql({
            "classpath:sql/create_sequence_different_types.sql",
            "classpath:sql/create-company.sql",
            "classpath:sql/insert_company.sql"
    })
    public class CompanyReadServiceTest {
    
    
        private static final Logger LOGGER = LoggerFactory.getLogger(CompanyReadServiceTest.class);
    
        private static String NAME_METHOD_READ_BY_NAME_BOOLEAN = "isByName";
    
    
        @Autowired
        private CompanyReadService companyReadService;
    
        @Autowired
        private CompanyReadRepositoryTest companyReadRepositoryTest;
    
    
        @Test
        public void getById() {
    ...
    

    在类启动期间,bean CompanyReadRepositoryTest将不会加入。

    JAVAlang.IllegalStateException:未能加载ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    

    ...

    原因:org。springframework。豆。工厂UnsatifiedDependencyException:创建名为“companyReadServiceTest”的bean时出错:通过字段“companyReadService”表示未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException:没有类型为“com”的合格bean。服务阅读公司CompanyReadService'可用:至少需要1个符合autowire候选资格的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}。springframework。豆。工厂注释。AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement。注射

    补充

    我不太懂

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = WebSpringBootJarApplication.class )
    @AutoConfigureTestDatabase(replace = NONE)
    @Sql({
            "classpath:sql/create_sequence_different_types.sql",
            "classpath:sql/create-company.sql",
            "classpath:sql/insert_company.sql"
    })
    @TestPropertySource(
            locations = "classpath:application-integration-test.properties")
    public class CompanyReadServiceTest {
    
    
        private static final Logger LOGGER = LoggerFactory.getLogger(CompanyReadServiceTest.class);
    
        private static String NAME_METHOD_READ_BY_NAME_BOOLEAN = "isByName";
    
        @Autowired
        private CompanyReadService companyReadService;
    
        @Autowired
        private CompanyReadRepositoryTest companyReadRepositoryTest;
    
    
        @Test
        public void getById() {
    
    ...
    

    现在是...

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.web.service.read.company.CompanyReadServiceTest': Unsatisfied dependency expressed through field 'companyReadRepositoryTest'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.web.dao.repository.company.CompanyReadRepositoryTest' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    

    知道错误是什么吗?拜托。

  • 共有2个答案

    洪和风
    2023-03-14

    在您的例子中,CompanyReadRepositoryTest没有使用任何bean定义注释(如@Component和扩展它的那些注释)进行注释。Spring无法识别任何所需类型的bean,并且无法将其注入到您的属性中。

    正如你所看到的,有“NoSuchBean定义例外”,说没有一个bean有资格成为自动接线候选人。

    只要用@Component(或任何bean定义注释)注释您的CompanyReadRepositoryTest,就可以了。

    在您的例子中,最好的注释是@Repository。

    查看@Repository和其他bean定义注释之间的区别。

    胡景焕
    2023-03-14

    解决方案

    你需要移动刀。根测试目录中的存储库。这个目录是com。

    src/test/java/com/ao/repository

    您重复道模块中存储库的位置。

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Sql({
            "classpath:sql/create_sequence_different_types.sql",
            "classpath:sql/create-company.sql",
            "classpath:sql/insert_company.sql"
    })
    @TestPropertySource(
            locations = "classpath:application-integration-test.properties")
    public class CompanyReadServiceTest {
    ...
    
     类似资料:
    • 我能够部署一个RESTEasy应用程序,可以很好地使用Weld(这意味着我的CDI可以工作),但我在集成测试中遇到了一些麻烦。我得到这个错误: 在测试时: 日志中的最后一条消息是

    • 我试图为一个Spring引导项目写一个集成测试。不幸的是,我对实现感到困惑。 下面是已尝试的示例代码段 问题 我是否需要一个单独的,带有注释以支持集成测试

    • 我正在测试我的spring boot应用程序。我正在做集成测试,但由于url返回的响应无法jsonfied,测试一直失败。请帮忙。以下是测试功能: 错误:

    • Spring Boot应用程序在启动服务器时抛出异常。 异常是: 上下文初始化期间遇到异常-取消刷新尝试:org。springframework。豆。工厂未满足的依赖项异常:创建名为“userController”的bean时出错:未满足的依赖项通过字段“userService”表示;嵌套的异常是org。springframework。豆。工厂未满足的依赖项异常:创建名为“userService”

    • 需要对Oauth2客户端的集成测试的帮助。 设置: 具有受保护UI和API的客户端 完成所有密码验证并检索访问令牌的身份验证服务器 集成测试: 放心用于终点测试 在实现Oauth2之前,测试工作良好 Ole测试示例: 问题: 如何使此测试再次工作? 应如何更改res-assured设置以支持OAuth2? 是否需要模拟身份验证服务器,或者是否可以注入/mock安全上下文?