Mocking static methods not working when initializing HttpServlet with @Tested #255

Closed
Spookyguy opened this Issue Feb 11, 2016 · 1 comment

Projects

None yet

2 participants

@Spookyguy

In the following code, InetAddress does not get mocked. The call of InetAddress#getByName in class Servlet.java runs the method of the real class (which fails, because of 'managementHost' is not found).

I suppose that the mock is not set up before Servlet#init is called by the routine which is used to initialize objects annotated with @Tested.

With JMockit 1.19 all works well and I learned that Release 1.19 did not call Servlet#init while initializing the object servlet, but since 1.20 it does.

`
public class MockTest {

@Tested
private Servlet servlet;

@Mocked
private InetAddress inetAddress;

@Before
public void recordConfig() throws UnknownHostException {

    new NonStrictExpectations() {
        {
            InetAddress.getByName(anyString);
            result = inetAddress;
        }
    };

}

@Test
public void mockTest() throws Exception {

    //        classInTheMiddle.init(servletConfig);

}

}
`

`public class Servlet extends HttpServlet {

@Override
public synchronized void init(ServletConfig conf) throws ServletException {
    try {
        final InetAddress inetAddress = InetAddress.getByName("managementHost");
    } catch (final UnknownHostException e) {
        throw new ServletException(e.getMessage(), e);
    }
}

}`

@rliesenfeld rliesenfeld added the bug label Feb 11, 2016
@rliesenfeld rliesenfeld self-assigned this Feb 11, 2016
@rliesenfeld
Member

Seems like a bug; thanks for reporting.

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