简体   繁体   中英

Multi-module Maven project - include test beans in jar

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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