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

Spookyguy opened this Issue Feb 11, 2016 · 1 comment


None yet

2 participants


In the following code, InetAddress does not get mocked. The call of InetAddress#getByName in class 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 {

private Servlet servlet;

private InetAddress inetAddress;

public void recordConfig() throws UnknownHostException {

    new NonStrictExpectations() {
            result = inetAddress;


public void mockTest() throws Exception {

    //        classInTheMiddle.init(servletConfig);



`public class Servlet extends HttpServlet {

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

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