jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
465 stars 240 forks source link

Enum value corrupted after recording expectations in 1.13 #94

Closed b2064832 closed 9 years ago

b2064832 commented 10 years ago

JMockit 1.13 corrupts enum values after recording expectations. Version 1.12 is working. Failing test:

import mockit.Mocked; import mockit.NonStrictExpectations; import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class JMockitEnumTest { public static enum MyEnum { FOO("foo"); private final String value;

    private MyEnum(final String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

public static interface Mockable {
    MyEnum getMyEnum();
}

@Test
public void test(@Mocked final Mockable mockable) throws Exception {
    assertEquals(MyEnum.FOO.getValue(), "foo");
    new NonStrictExpectations() {{ //or Expectations
        mockable.getMyEnum();
        result = MyEnum.FOO;
    }};
    assertEquals(MyEnum.FOO.getValue(), "foo", "Enum value corrupted after mocking."); // fails here, value null
}

}

rliesenfeld commented 10 years ago

The enum value is there, but the MyEnum.getValue() method gets mocked, so it returns null. If cascading is disabled (with "@Mocked(cascading = false)"), then it doesn't get mocked and the test passes.

This said, since "mockable.getMyEnum()" was explicitly recorded to return a still unmocked enum element, it should have remained unmocked. I will fix that.