Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cobertura instrumentation Error #132

Closed
liscju opened this issue Jan 15, 2016 · 4 comments
Closed

Cobertura instrumentation Error #132

liscju opened this issue Jan 15, 2016 · 4 comments
Labels

Comments

@liscju
Copy link

liscju commented Jan 15, 2016

Its probably the same problem as it was in #126, using EqualsVerifier with Powermock leads to problem that equals method and hashCode is not covered in cobertura. I minimalized problem to to this: i have one class:

public class ClassToTest {
    private int id;
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ClassToTest that = (ClassToTest) o;

        if (id != that.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id;
    }

    public int add(int i, int j) {
        return i + j;
    }
}

and i want to test it with:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassToTest.class})
public class SimpleClassTest {
    @Test
    public void testEqualsAndHashCode() {
        EqualsVerifier
                .forClass(ClassToTest.class)
                .usingGetClass()
                .suppress(Warning.NONFINAL_FIELDS)
                .verify();
    }

    @Test
    public void addTest() {
        ClassToTest classToTest = new ClassToTest();
        classToTest.add(5,9);
    }
}

Running cobertura on this returns:

Running com.puppycrawl.tools.checkstyle.filters.SimpleClassTest
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.016 sec <<< FAILURE!
testEqualsAndHashCode(com.puppycrawl.tools.checkstyle.filters.SimpleClassTest)  Time elapsed: 1.231 sec  <<< FAILURE!
java.lang.AssertionError: Non-nullity: equals throws NullPointerException on field __cobertura_counters.
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
        at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:406)
        at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:389)
        at com.puppycrawl.tools.checkstyle.filters.SimpleClassTest.testEqualsAndHashCode(SimpleClassTest.java:23)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMock
JUnit44RunnerDelegateImpl.java:310)
        at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
        at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJU
nit44RunnerDelegateImpl.java:294)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(Powe
rMockJUnit47RunnerDelegateImpl.java:127)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJU
nit47RunnerDelegateImpl.java:82)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenA
fters(PowerMockJUnit44RunnerDelegateImpl.java:282)
        at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.
java:207)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:1
46)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
        at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
        at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
        at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.NullPointerException
        at com.cobertura.failed.ClassToTest.equals(ClassToTest.java:10)
        at nl.jqno.equalsverifier.NullChecker$NullPointerExceptionFieldCheck$1.run(NullChecker.java:65)
        at nl.jqno.equalsverifier.NullChecker$NullPointerExceptionFieldCheck.handle(NullChecker.java:88)
        at nl.jqno.equalsverifier.NullChecker$NullPointerExceptionFieldCheck.execute(NullChecker.java:62)
        at nl.jqno.equalsverifier.FieldInspector.check(FieldInspector.java:37)
        at nl.jqno.equalsverifier.NullChecker.check(NullChecker.java:44)
        at nl.jqno.equalsverifier.EqualsVerifier.verifyWithoutExamples(EqualsVerifier.java:429)
        at nl.jqno.equalsverifier.EqualsVerifier.performVerification(EqualsVerifier.java:416)
        at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:386)
        ... 36 more

[Some other things]
[INFO] Cobertura Report generation was successful.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.668 s
[INFO] Finished at: 2016-01-15T12:12:24+01:00
[INFO] Final Memory: 25M/216M
[INFO] ------------------------------------------------------------------------

After looking at coverage report the equals method and hashCode is not covered. It is especially misleading in big projects because cobertura report builds succesfully, the output of command is large so you dont see error that was thrown in the middle of huge report. I prepared minimalized project in which this bug occures in https://github.com/liscju/CoberturaCoverageFailed , running it with

mvn cobertura:cobertura

leads to error.

@jqno
Copy link
Owner

jqno commented Jan 15, 2016

Thank you for the minimalized project! I've reproduced the problem.

I'll take a closer look and let you know as soon as I know more.

@jqno
Copy link
Owner

jqno commented Jan 18, 2016

I've just released a new version for EqualsVerifier which fixes this issue. Check out the pull request on your minimized project!

@jqno jqno closed this as completed Jan 18, 2016
@liscju
Copy link
Author

liscju commented Jan 18, 2016

@jqno i checked fix in minimalized project and in checkstyle and now its working correctly, many thanks

@jqno
Copy link
Owner

jqno commented Jan 18, 2016

Cool! You're welcome, and thanks again for building the minimalized project, it saved soooo much time for me. I wish everybody did that :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants