繁体   English   中英

Spring集成测试中的@Autowired和UnsatisfiedDependencyException

[英]@Autowired and UnsatisfiedDependencyException in Spring integration test

我有带有 Spring Boot 和外部服务器 Weblogic 的多模块项目。

这些是模块:

  • 服务
  • 网络

    pom.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>

  • 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
         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>

  • pom.xml ( 网页 )

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

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

<?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>

  • 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>

    <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 模块的是 test-dir。

  • 源代码/测试/java/com/web

这是一个存储库

  • src/test/java/com/web/dao/repository/company/CompanyReadRepositoryTest.java
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不会加入。

java.lang.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.beans.factory.UnsatisfiedDependencyException:创建名为“companyReadServiceTest”的bean时出错:通过字段“companyReadService”表达的不满意的依赖; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“com.service.read.company.CompanyReadService”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。 依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject

添加

我很少做

@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)}

任何想法是什么错误? 请。

解决方案

您需要将 dao.repository 移动到根 test-dir 中。 这个目录是 com.

src/test/java/com/dao/repository

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

@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 {
...

在您的情况下, CompanyReadRepositoryTest 没有使用任何 bean 定义注释(如 @Component 和扩展它的注释)进行注释。 Spring 无法识别所需类型的任何 bean,也无法将其注入您的属性中。

如您所见,有“NoSuchBeanDefinitionException”,表示没有一个 bean 有资格作为自动装配候选者。

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

在您的情况下,最好的注释是@Repository。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM