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

@Mocked not wrorking with abstract Base class #276

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

Comments

2 participants
@numeralEasy

numeralEasy commented May 2, 2016

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

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld May 2, 2016

Member

Indeed, it's a bug.

Thanks for reporting!

Member

rliesenfeld commented May 2, 2016

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