jmockit / jmockit1

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

Unable to record a mocked class instance for a cascading mock #83

Closed jOlliv closed 9 years ago

jOlliv commented 9 years ago

The following code runs fine with JMockit 1.11 but stop working with JMockit 1.12 with a NullPointerException at java.io.Writer.write():

import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.junit.Test;
import mockit.Injectable;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.Verifications;

public class MockingFacesContextTest {
    @Injectable private ResponseWriter mockResponseWriter;
    @Mocked private FacesContext mockFacesContext;

    @Test public void test() throws Exception {
        new NonStrictExpectations() {{
            FacesContext.getCurrentInstance();
            result = mockFacesContext;
            mockFacesContext.getResponseWriter();
            result = mockResponseWriter;
        }};

        FacesContext.getCurrentInstance().getResponseWriter().write( "test" );

        new Verifications() {{
            mockResponseWriter.write( "test" );
        }};
    }
}

Setting "cascading=false" to FacesContext let the test run fine with JMockit 1.12.

Making usage of cascading will also not work for this test with JMockit 1.12:

@Injectable private ResponseWriter mockResponseWriter;
@Mocked private FacesContext mockFacesContext;

@Test public void test() throws Exception {
    new NonStrictExpectations() {{
        FacesContext.getCurrentInstance().getResponseWriter();
        result = mockResponseWriter;
    }};

    FacesContext.getCurrentInstance().getResponseWriter().write( "test" );

    new Verifications() {{
        mockResponseWriter.write( "test" );
    }};
}
evdzhan commented 9 years ago

Check issue #70 that I have opened. You cannot mock java classes with JMockit 1.12. I believe this will be possible again with JMockit 1.13.

rliesenfeld commented 9 years ago

Same cause as #81. Workaround: simply remove the entire expectation block.