Run unit tests with dependencies that are in the exclusions group

Asked

Viewed 67 times

1

I have a scenario where I need to run a test with Junit in a feature that has an external dependency, but to run this functionality on the application server I need to put this external dependency in the group of exclusions in my pom.xml. Only in the test I don’t need/don’t have the container of Jboss EAP 5 started, which already has this dependency. And at this point the test accuses that the dependency is not available.

Does anyone know any alternative to run unit test with external dependency, and when packaging my project in a jar/War perform the deletion properly.

Below I put the dependency in question:

<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.21</version>
    <exclusions>
        <exclusion>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
        </exclusion>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

1 answer

2


Try explicitly adding the two transitive dependencies you deleted when adding nekohtml, defining scope test for them:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>xml-apis</groupId>
    <artifactId>xml-apis</artifactId>
    <scope>test</scope>
</dependency>

Note the tag I added, <scope>test</scope>. This will make this dependency available during testing and keep the dependency out of the distribution package.

If these dependencies are also required for the production code and not just for the test code, you can use the scope provided (<scope>provided</scope>), thus dependencies will be used during compilation and during testing, but will not be included in the distribution package.

  • If I do this, Nekohtml won’t be available in my package, and the only dependency I don’t need in Runtime is xerces/xml-Apis.

  • @eliocapelati I think I understand your need. I updated my answer.

  • Thanks @Caffé, I had already considered this alternative, I thought I had something specific to declare the dependencies selectively... Anyway thank you very much for the reply! I ended up using the scopo provided which has best fit with the Maven documentation: http://books.sonatype.com/mvnref-book/reference/pom-relationships-sect-project-dependencies.html#pom-relationships-sect-dependency-Scope

Browser other questions tagged

You are not signed in. Login or sign up in order to post.