@Mocked not wrorking with abstract Base class #276

Closed
numeralEasy opened this Issue May 2, 2016 · 1 comment

Projects

None yet

2 participants

@numeralEasy

Jmockit Version 1.20+

You restored original mocking semantics when an expectation is recorded on a mocked base class:
#236
It was not restored for mocked abstract base class.

Abstract base class works with @Capturing and partial mocking. @Mocked doesn't work.

public final class TestSubClass {

    public static abstract class BaseClass {

        String name;

        public String getName() {
            return name;
        }

        public void setName(String n) {
            name = n;
        }

    }

    final static class SubClass extends BaseClass {

    }

    /**
     * with jomckit version 1.20+ :
     *   -fails if @Mocked used on abstract base class
     *   -works if @Mocked used on non-abstract base class
     * 
     * with jmockit version 1.19 : 
     *   -this test works
     * 
     * @param baseClass
     */
    @Test
    public void testGetName_fails(final @Mocked BaseClass baseClass) {

        new Expectations() {

            {
                baseClass.getName();
                result = "mocked name";
            }
        };

        assertEquals("mocked name", new SubClass().getName());
    }

    /**
     * works with abstract and non-abstract base class
     * @param baseClass
     */
    @Test
    public void testGetName_works(final @Capturing BaseClass baseClass) {

        new Expectations() {

            {
                baseClass.getName();
                result = "mocked name";
            }
        };

        assertEquals("mocked name", new SubClass().getName());
    }

    /**
     * partial mocking works with abstract and non-abstract base class
     * @param baseClass
     */
    @Test
    public void testGetName_works2() {

        final SubClass subClass = new SubClass();
        new Expectations(subClass) {

            {
                subClass.getName();
                result = "mocked name";
            }
        };

        assertEquals("mocked name", subClass.getName());
    }
}
@rliesenfeld rliesenfeld self-assigned this May 2, 2016
@rliesenfeld rliesenfeld added the bug label May 2, 2016
@rliesenfeld
Member

Indeed, it's a bug.

Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment