【问题标题】:Multi-module Maven project - include test beans in jar多模块 Maven 项目 - 在 jar 中包含测试 bean
【发布时间】:2021-01-19 16:49:18
【问题描述】:

如何在导入到另一个模块的 jar 中包含测试目录 (src/test/) 下的 Spring bean?

我有几个 Maven 项目:ms1、ms2、ms3 和 general-shared。 general-shared 是一个多模块的 Maven 项目。它包含几个模块:general-shared-utills、general-shared-fs 等...

在每个子模块中,我都有一个专用的 pom.xml,其中包含所需的所有依赖项,在通用共享 pom 中,我有关于构建和模块的详细信息:

  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <modules>
        <module>general-shared-utills</module>
        <module>general-shared-fs</module>
    </modules>

似乎我为子模块中的测试(位于 src/test 中)创建的所有 bean 都没有导入到 Maven 模块(例如 ms1)。

一个例子:

在子模块 general-shared-utills 我有以下类:

在 /src/main 下:

@Component
public class Shop
    @Autowired
    private AWSService awsService

@Component
@Profile("prod")
public class AWSService 
...

在/src/test下:

@Component
@Profile("test")
public class AWSServiceMock extends AWSService 

当我在 ms1 的 pom 中导入 general-shared-utills 模块时,我希望它包含 AWSServiceMock 类。现在,ms1 的测试找不到任何 AWSService 类型的 bean,因为它没有带有测试配置文件的 AWSService 类型的 bean:

 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'a.x.y.aws.AWSService ' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

【问题讨论】:

    标签: java spring maven


    【解决方案1】:

    我将相关的 bean(模拟)从 src/test 目录移到了 src/main。

    我还在 maven 的网站上找到了 this solution

    如果有人有另一个实用的答案,我会很高兴听到并更新帖子的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 2018-07-11
      • 2010-12-16
      相关资源
      最近更新 更多