Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection and Tests

I have a project that contains some tests, all it does is create a test-jar to be run by other projects. And it depends on a another artifact, let's call it artifact X

then there are multiple other projects that run the test-jar, using the maven-surefire-plugin

I have multiple implementations of artifact X and what I want to do is to be able to run those tests using the implementation that I want, not X

so for example:

in project A I want to run the tests using implementation A

in project B I want to run the tests using implementation B

this does not seems to be possible, because once the test-jar generated, it has the artifact X hard-coded in it

so I'm open to any other solutions

Edit :

extra information:

the pom file of the project generating the tests :

the part for generating the test-jar

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

the Artifact X dependency :

<dependency>
        <groupId>com.conztanz</groupId>
        <artifactId>X</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

the pom file of one of the projects running the tests (project A):

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.15</version>
            <configuration>
                <dependenciesToScan>
                    <dependency>testJar</dependency>
                </dependenciesToScan>
            </configuration>
        </plugin>
    </plugins>
</build>

Ideally what I want is do some thing that tells project A to Override artifact X with it's own implementation

I hope this makes more sense now

like image 596
isco Avatar asked Mar 04 '26 03:03

isco


1 Answers

I think you're looking for the scope called provided.

The dependency would look like this:

<dependency>
    <groupId>com.conztanz</groupId>
    <artifactId>X</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>provided</scope>
</dependency>

This tells Maven: "Hey, I need this to compile, but don't include it in the target output, because a container (or something) will provide the jar itself."

like image 140
Stewart Avatar answered Mar 05 '26 16:03

Stewart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!