0

I have a multi-module Maven project (Spring). One of the modules (A) depends on another (B). A has tests and those tests reference classes in B.

That all works fine.

However, B also has some specific configuration only for testing. When A references B during testing, only the Main (not Test) resources are available.

I want to be able to reference B's resources.

In particular, I'm trying to reference Spring's context for B while running tests in A.

Ryan Griffith
  • 1,481
  • 15
  • 37
  • When building `B` you can create a "test-jar". Then `A` can depend on that with scope "test". https://stackoverflow.com/questions/174560/sharing-test-code-in-maven has more info. – Erik Mar 10 '21 at 03:54
  • @Erik, I read this SO question you referenced. However, do I need to do this? I'm able to reference `B` just fine, so there already is some mechanism for `A` to reference `B`.... I'd prefer to not have to create a JAR when I already can reference the logic within `B` and am now just needing the resources. Side note: I don't see `B`'s artifacts anywhere within `A`s `target` folder - not sure how `A` accesses `B` in the first place, but it does work. – Ryan Griffith Mar 10 '21 at 04:24
  • Simply make a separate module which puts the needed resources into `src/main/resources` and use it with scope:test – khmarbaise Mar 10 '21 at 09:33

1 Answers1

2

As Erik has explained, the solution has two steps:

  1. All the test sources and code of module B (everything under B's src/test/*) should be packed into a special artifact "test-jar". This can be done by configuring the jar plugin

Put the following into B's pom.xml file:

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

Now when you package B in its target directory you'll find two jars - one for regular sources (this one goes to production) and the one with tests

  1. Now A should depend on this B's test jar regardless the regular dependency of A depends on B. Since maven has a concept of scopes, in this case A should depend on test-b in the scope of "test" so that this test-b artifact won't propagate to production (its not transitive if A by itself is a Jar and maven from add the test-b into, say WAR, if A is a war for example). So you place the following in the A's pom.xml this time:

<dependencies>
   <!-- You should already have a regular dependency on B: -->
   <dependency>
     <groupId><GROUP_ID_OF_B_GOES_HERE></groupId>
     <artifactId><ARTIFACT_ID_OF_B_GOES_HERE></artifactId>
     <version><VERSION_GOES_HERE></version>
   </dependency>

   <!-- Add the following dependency: --> 
   <dependency>
     <groupId><GROUP_ID_OF_B_GOES_HERE></groupId>
     <artifactId><ARTIFACT_ID_OF_TEST_B_GOES_HERE></artifactId>
     <version><VERSION_GOES_HERE></version>
     <scope>test</scope>  <!-- this is the scope I was talking about, test-b is visible to a only during the tests -->
   </dependency>
   ...
</dependencies>

Of course fill the placeholders its just an example.

Mark Bramnik
  • 31,144
  • 4
  • 41
  • 69