多模块Maven项目-在jar中包含测试bean

时间:2020-10-04 18:06:01

标签: java spring maven

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

我有几个Maven项目:ms1,ms2,ms3和常规共享项目。通用共享的是一个多模块的Maven项目。它包含一些模块:通用共享实用程序,通用共享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>

似乎我在子模块中为测试创建的所有bean(位于src / test中)都没有导入到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)}

1 个答案:

答案 0 :(得分:0)

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

我还在maven网站上发现了this solution

如果有人会有其他实用答案,我将很乐意听到并更新帖子的答案。

相关问题