Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Spookyguy opened this issue Feb 11, 2016 · 1 comment


Copy link

@Spookyguy Spookyguy commented Feb 11, 2016

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 self-assigned this Feb 11, 2016
Copy link

@rliesenfeld rliesenfeld commented 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

No branches or pull requests

2 participants