5

Getting below error on invoking TestNG unit test cases with clover profile enabled. Did not change any clover version. Not getting any clue why its happening.

Here is my libs details - clover-4.0.3, testng - 6.0.1, maven 3

Caused by: java.lang.NoClassDefFoundError: com_atlassian_clover/TestNameSniffer
at com.mds404.catalog.infrastructure.entity.ModelBase.<clinit>(ModelBase.java:27)
at sun.reflect.GeneratedSerializationConstructorAccessor1.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:111)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:51)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:52)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:41)
at org.mockito.Mockito.mock(Mockito.java:1014)
at org.mockito.Mockito.mock(Mockito.java:909)
at com.mds404.rest.v2.controller.BaseControllerTest.<init>(BaseControllerTest.java:107)
... 33 more
Caused by: java.lang.ClassNotFoundException: com_atlassian_clover.TestNameSniffer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.maven.surefire.booter.IsolatedClassLoader.loadClass(IsolatedClassLoader.java:97)
... 46 more
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
mds404
  • 371
  • 4
  • 9
  • That's not even a valid class name. Change the underbars and the slash to dots. – user207421 Jan 18 '17 at 00:18
  • @EJP It actually is a real class name. As to why it's named so confusing I have no idea. Shame on you, Atlassian. – xiaofeng.li Jan 18 '17 at 00:34
  • The one with the slash is not a real or even legal class name. – user207421 Jan 18 '17 at 00:53
  • 1
    Hi, it's Clover developer here :) The com_atlassian_clover package is a perfectly valid Java package name, although indeed unusual. The reason why we used underscores instead of dots was to ensure that the code will not fail to compile when someone creates a class named "com". And this is not a theoretical problem - we saw code having classes named "com" or "java" ... :-) – Marek Jan 23 '17 at 10:47

3 Answers3

6

The error

Caused by: java.lang.NoClassDefFoundError: com_atlassian_clover/TestNameSniffer

means that the Clover JAR (com.atlassian.clover:clover) is missing on the class path.

This is usually caused by two issues:

  1. Missing com.atlassian.clover:clover dependency. Please note that the Clover Maven Plugin adds this dependency to the project automatically (during the build). However, in some cases, you have to add this dependency manually to other Maven plugins - usually to those which fork new JVM - for instance for in-container tests.

  2. Build is running with Clover disabled but still failing on this error. This usually happens if you run the build with Clover enabled via 'clover:setup' and next install (or deploy) JARs created. When you run your build again, but this time with Clover disabled, the build can fetch these JARs from your ~/.m2 cache. As these JARs contain instrumented code and com.atlassian.clover:clover is missing, build will fail. To solve this problem, clean up your ~/.m2 cache and don't use "clover:setup" with "install" or "deploy".

Reference:

Cheers Marek

Marek
  • 748
  • 6
  • 13
  • 1
    This was tremendously helpful, specifically the mention of in-container tests. That helped me add the dependency to the correct Gradle dependency scope. Thank you! – Philip Tenn May 15 '18 at 20:20
0

So finally what did help me here is adding another dependency of clover in dependencies tag. I already had this dependency as part of clover profile. But still I was getting above reported error.

After spending few hours when I added one more time same dependency next to all other libraries. As I was out of thoughts. And magically it did work.

Here is my updated pom.xml with both dependency copies -

...
<profile>
<id>clover</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <build>
        <plugins>
            <plugin>
                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>maven-clover2-plugin</artifactId>
                <version>4.0.3</version>
                <configuration>
                    <methodContexts>
                            <toStr>(.* )?public .*(toString).*</toStr>
                            <hashCode>(.* )?public .*(hashCode).*</hashCode>
                            <equals>(.* )?public boolean equals\(Object.*\).*</equals>
                            <privateConstructors>(.* )?private +[a-zA-Z0-9]+ *\(\).*</privateConstructors>
                    </methodContexts>
                    <contextFilters>toStr,hashCode,equals,privateConstructors</contextFilters>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.atlassian.clover</groupId>
            <artifactId>clover</artifactId>
            <version>4.0.3</version>
        </dependency>
    </dependencies>
</profile>

And then all other dependencies -

 ....
<dependencies>
  <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>${org.springframework.version}</version>
  </dependency>
  <dependency>
    <groupId>com.atlassian.clover</groupId>
    <artifactId>clover</artifactId>
    <version>4.0.3</version>
</dependency>
...
mds404
  • 371
  • 4
  • 9
0

I stumbled across this error, that is, running my unit test I got

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.082 s <<< FAILURE! - in [...]
[ERROR] [...]  Time elapsed: 0.017 s  <<< ERROR!
java.lang.NoClassDefFoundError: com_atlassian_clover/TestNameSniffer
    at [...]
Caused by: java.lang.ClassNotFoundException: com_atlassian_clover.TestNameSniffer
    at [...]

First, let me say that this error really puzzled me, since I was sure that the same configuration of the project did not had this error before.

Here is what caused this error:

  • The project that exhibits the error references a dependency which I own or which I build and installed in my maven repository.
  • That dependency was mvn clean install-ed during a "Clover" run, that is, the version in .m2/repository had some clover code compiled into it.

Of course, this resulted in the other project pulling in some clover-instrumented code, without the required dependency.

The issue disappeared, once I have removed the dependency from my .m2/repository such that it was pulled from Maven central (which was clean and did not had the clover code).

Edit: Just saw that his was the second case in Mareks answer.

Christian Fries
  • 16,175
  • 10
  • 56
  • 67