Assertj-core: 3.4.0: SoftAssertions + Comparable = CodeGenerationException

Created on 5 Apr 2016  路  4Comments  路  Source: assertj/assertj-core

After upgrading from AssertJ 3.3.0 to AssertJ 3.4.0, I am getting a CodeGenerationException when using SoftAssertions on an object that implements the Comparable interface.

Note: I ran the test within Eclipse 4.4.2 with Java 8 in a fresh project to whose .classpath I had manually added the assertj-core-3.4.0.jar obtained by running mvn install in the checked out repository.

Code:

import org.assertj.core.api.SoftAssertions;
import org.junit.Test;

public class ExampleTest {

    @Test
    public void exampleTestCase() {
        SoftAssertions softly = new SoftAssertions();
        Example example = new Example(0);
        softly.assertThat(example).isEqualTo(example);
        softly.assertAll();
    }

    class Example implements Comparable<Example> {

        int id;

        Example(int id) {
            this.id = id;
        }

        @Override
        public int compareTo(Example that) {
            return this.id - that.id;
        }
    }

}

Exception:

org.assertj.core.internal.cglib.core.CodeGenerationException: java.lang.NoSuchMethodException-->org.assertj.core.api.AbstractComparableAssert$$EnhancerByCGLIB$$4b429892.<init>(java.lang.Comparable)
    at org.assertj.core.internal.cglib.core.ReflectUtils.getConstructor(ReflectUtils.java:255)
    at org.assertj.core.internal.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:227)
    at org.assertj.core.internal.cglib.proxy.Enhancer.createUsingReflection(Enhancer.java:650)
    at org.assertj.core.internal.cglib.proxy.Enhancer.firstInstance(Enhancer.java:549)
    at org.assertj.core.internal.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:244)
    at org.assertj.core.internal.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
    at org.assertj.core.internal.cglib.proxy.Enhancer.create(Enhancer.java:305)
    at org.assertj.core.api.SoftProxies.create(SoftProxies.java:38)
    at org.assertj.core.api.AbstractSoftAssertions.proxy(AbstractSoftAssertions.java:26)
    at org.assertj.core.api.AbstractStandardSoftAssertions.assertThat(AbstractStandardSoftAssertions.java:166)
    at ExampleTest.exampleTestCase(ExampleTest.java:10)
    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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NoSuchMethodException: org.assertj.core.api.AbstractComparableAssert$$EnhancerByCGLIB$$4b429892.<init>(java.lang.Comparable)
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.assertj.core.internal.cglib.core.ReflectUtils.getConstructor(ReflectUtils.java:251)
    ... 33 more
bug

All 4 comments

Thanks for reporting this
I was able to reproduce it, I'm still investigating to find a solution.

Fixed for 2.4.1/3.4.1

In the meantime, just cast your instance to Object to force using a correctly soft assertThat method :

softly.assertThat((Object)example).isEqualTo(example);

Sorry everybody, the bug was my fault. It's what you get for doing even a small change without a test. :(

Was this page helpful?
0 / 5 - 0 ratings