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

equals / toString not called on superclass if called on childclass that has a mocked "sibling" class #63

Closed
cm-rudolph opened this issue Sep 13, 2014 · 1 comment
Assignees
Labels
bug

Comments

@cm-rudolph
Copy link

@cm-rudolph cm-rudolph commented Sep 13, 2014

The following test fails, if ChildClassA is mocked (using for example @Injectable or @Mocked).
If I try to debug the running test, I see that the parent equals method isn't executed. toString doesn't get executed on the parent class, too.
If I remove the unused parameter or try to compare two instances of ChildClassA, everything works as expected.

import mockit.Injectable;
import mockit.integration.junit4.JMockit;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;

@RunWith(JMockit.class)
public class InheritanceTest {
    @Test
    public void testThatFails(@Injectable ChildClassA notUsed) {
        assertEquals(new ChildClassB(1), new ChildClassB(1));
    }

    @Test
    public void testThatSucceeds() {
        assertEquals(new ChildClassB(1), new ChildClassB(1));
    }

    @Test
    public void testThatSucceedsToo(@Injectable ChildClassA notUsed) {
        assertEquals(new ChildClassA(1), new ChildClassA(1));
    }

    public static class ParentClass {
        private final int id;

        public ParentClass(int id) {
            this.id = id;
        }

        @Override
        public boolean equals(Object o) {
            return o instanceof ParentClass && id == ((ParentClass) o).id;
        }

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

        @Override
        public String toString() {
            return "id = " + id;
        }
    }

    public static class ChildClassA extends ParentClass {
        public ChildClassA(int id) {
            super(id);
        }
    }

    public static class ChildClassB extends ParentClass {
        public ChildClassB(int id) {
            super(id);
        }
    }
}

I tested using JMockit 1.11, JUnit 4.11 and Java 8u20.

@cm-rudolph

This comment has been minimized.

Copy link
Author

@cm-rudolph cm-rudolph commented Sep 14, 2014

A simple workaround for now is to add @Injectable ChildClassB workaround as parameter to testThatFails' signature.

@rliesenfeld rliesenfeld added the bug label Sep 19, 2014
@rliesenfeld rliesenfeld self-assigned this Sep 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.