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

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

Comments

2 participants
@Spookyguy

Spookyguy commented Feb 11, 2016

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

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Feb 11, 2016

Member

Seems like a bug; thanks for reporting.

Member

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