Question

I needed powermock with mockito api for some JUnit test. The tests are running fine, but now I cannot run the project. I don't understand it, because all new libraries are in the test scope:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.5.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.5.4</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
<!-- THIS IS NEEDED FOR JAVA 1.7 -->
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.18.1-GA</version>
        <scope>test</scope>
    </dependency>

When I deploy the application on Tomcat, I get this exception:

Caused by: java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile
at org.hibernate.ejb.packaging.AbstractJarVisitor.checkAnnotationMatching(AbstractJarVisitor.java:246)
at org.hibernate.ejb.packaging.AbstractJarVisitor.executeJavaElementFilter(AbstractJarVisitor.java:212)
at org.hibernate.ejb.packaging.AbstractJarVisitor.addElement(AbstractJarVisitor.java:173)
at org.hibernate.ejb.packaging.ExplodedJarVisitor.getClassNamesInTree(ExplodedJarVisitor.java:126)
...
Caused by: java.lang.ClassNotFoundException: javassist.bytecode.ClassFile
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:154

I don't understand why the test scope dependencies interfere with the deployment.

Was it helpful?

Solution

You dont really have enough info about your pom but this looks likely:

Javassist is a transitive dependency of Hibernate (see here), before you added powermock it was being included in your WAR file as a result of this dependency.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>XXXXX</version>
</dependency>

With your powermock trickery you have told Maven that javassist should not be included in your WAR file by explicitly making it test scope with:

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.18.1-GA</version>
    <scope>test</scope>
</dependency>

So, change the javassist scope to runtime and it will be included in your WAR file and your ClassNotFoundException should go away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top