jmockit / jmockit1

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

Regression: Calling constructor for MockUp in Test results in java.lang.VerifyError for 1.9 #25

Closed abc2mit closed 10 years ago

abc2mit commented 10 years ago

Issue: I have a mock class that extends MockUp. When running tests in JMockIt 1.9, the tests fail with java.lang.VerifyError. This is through Maven on OSX.

$ mvn --version
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T09:37:52-08:00)
Maven home: /usr/local/Cellar/maven/3.2.1/libexec
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8

OS name: "mac os x", version: "10.9.4", arch: "x86_64", family: "mac"

I rolled back JMockIt to 1.8 and the tests passed again. Also, tests seem to work in IntelliJ on 1.9 with standard configuration run. Code is below.

Created a mock class that mocks org.mongojack.DBCursor

import mockit.Mock;
import mockit.MockUp;
import org.mongojack.DBCursor;
import org.mongojack.JacksonDBCollection;

import java.util.List;

public class DBCursorMock<T> extends MockUp<DBCursor<T>> {
    private List<T> items = null;

    public DBCursorMock (List<T> items) {
        this.items = items;
    }

    @Mock
    public void $init (JacksonDBCollection<T, ?> jacksonDBCollection,
                       com.mongodb.DBCursor cursor) {

    }

    @Mock
    public List<T> toArray () {
        return items;
    }
}

Class is called in test like so:

    JacksonDBCollectionMock<User, ObjectId> collection = new JacksonDBCollectionMock<>();
    collection.setDBObjectItems(userList);
    new DBCursorMock<>(userList);

(Note: JacksonDBCollectionMock is another mock that does not have an $init method.)

Stack trace:

testGetPasscode(net.foodjudge.apiserver.db.DeviceAtomicsTest)  Time elapsed: 0.02 sec  <<< ERROR!
java.lang.VerifyError
    at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
    at net.foodjudge.mock.DBCursorMock.<init>(DBCursorMock.java:20)
    at net.foodjudge.apiserver.db.DeviceAtomicsTest.testGetPasscode(DeviceAtomicsTest.java:127)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
abc2mit commented 10 years ago

That's awesome! Thanks @rliesenfeld for fixing it so quickly!