英文:
Multi-module Maven project - include test beans in jar
问题
如何将位于测试目录(src/test/)下的Spring Beans 包含在导入到另一个模块的JAR中?
我有几个Maven项目:ms1、ms2、ms3和general-shared。general-shared是一个多模块的Maven项目。它包含了几个模块:general-shared-utills、general-shared-fs等等...
在每个子模块中,我有一个专用的pom.xml,其中包含了所有需要的依赖项,在general-shared的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)}
英文:
How can I include Spring beans under test dir (src/test/) in the jar that is imported to another module ?
I have a few Maven projects : ms1, ms2, ms3 and general-shared. The general-shared is a multi-module Maven project. It contains a few modules : general-shared-utills, general-shared-fs etc...
In each sub module I had a dedicated pom.xml with all the dependencies that are needed and in the general-shared pom I have details regarding the build and the modules :
<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>
It seems that all the beans that I created for tests (located in src/test) in the sub modules aren't imported to the Maven module (ms1 for example).
An example :
In sub module general-shared-utills I have the following classes :
Under /src/main :
@Component
public class Shop
@Autowired
private AWSService awsService
@Component
@Profile("prod")
public class AWSService
...
Under /src/test:
@Component
@Profile("test")
public class AWSServiceMock extends AWSService
When I import the general-shared-utills module in the pom of ms1 I want it to include the AWSServiceMock class. Right now, the tests of ms1 can't find any bean of type AWSService because it doesn't have a bean of AWSService type with test profile :
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
我将相关的bean(模拟对象)从src/test目录移动到了src/main目录。
我还在Maven的网站上找到了这个解决方案。
如果有人有另外一个实用的回答,我会很乐意听取并更新帖子的答案。
英文:
I moved the relevant beans(mocks) from the src/test dir to src/main.
I also found this solution on maven`s site.
if someone will have another practical answer I'll be happy to hear and update the post`s answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论