org.powermock.reflect.exceptions.MethodNotFoundException: - when mocking java sql classes

放肆的年华 提交于 2019-12-11 04:37:20

问题


I am trying to write unit test for a servlet containing a call to a Postgres database. I am mocking the Connection, Statement and ResultSet classes. However, any method calls on the mocks of those calls give me a MethodNotFoundException. There are some references where it mentions some functionality was broken with Java 8 and PowerMock 1.5.x but I am on PowerMock 1.6.6. Any help is highly appreciated.

Here's the code: public class CitiesServlet extends HttpServlet {

private static final Logger log = LoggerFactory.getLogger(CitiesServlet.class);

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    log.debug("doGet called on servlet=" + this.getClass());
    PrintWriter writer = ServletUtils.setResponseParametersAndGetPrinter(resp);
    CitiesWrapperModel citiesWrapperModel = new CitiesWrapperModel();
    String message = ServletHelper.getInstance().openDBConnection("PgBundle");
    if (!message.startsWith("Servus")) {
        writer.println("<h1>Database connection failed to open " + message + "</h1>");
        return;
    }
    CitiesData citiesData = new CitiesData();
    citiesData.setCities(getCities());
    citiesWrapperModel.setData(citiesData);
    ServletUtils.writeResultsAndCloseStream(writer, citiesWrapperModel);
}

public List<String> getCities() {
    List<String> cities = new ArrayList<>();

    String query = "SELECT * FROM cities";
    try {
        Connection connection =  ServletHelper.getInstance().getConnection();
        Statement st = connection.createStatement();
        ResultSet rs = st.executeQuery(query);
        while (rs.next()) {
            String cityName = rs.getString("cityName");
            cities.add(cityName);
        }
        st.close();
        rs.close();
    } catch (SQLException sqe) {
        log.warn("SQL exception when accessing users table, sql message= " + sqe.getMessage());
    }
    return cities;
}}

Test class:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ServletHelper.class, Statement.class, ResultSet.class, Connection.class})
public class CitiesServletITTest {

private HttpServletRequest testRequest;
private HttpServletResponse testResponse;
private ServletHelper mockServletHelper;
private Connection mockConnection;
private CitiesServlet citiesServlet;

@Before
public void setUp(){
    citiesServlet = new CitiesServlet();
    testRequest = new TestHttpServletRequest();
    testResponse = new TestHttpServletResponse();
    mockServletHelper = mock(ServletHelper.class);
    mockConnection = mock(Connection.class);

}

@Test
public void canGetCitiesData() throws ServletException, IOException, SQLException {
    PowerMockito.mockStatic(ServletHelper.class);
    PowerMockito.when(ServletHelper.getInstance()).thenReturn(mockServletHelper);
    when(mockServletHelper.openDBConnection(Mockito.anyString())).thenReturn("Servus");
    when(mockServletHelper.getConnection()).thenReturn(mockConnection);


    ResultSet resultSet = Mockito.mock(ResultSet.class);
    Mockito.when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(resultSet.getString("cityName")).thenReturn("Philadelphia").thenReturn("Boston").thenReturn("New York");

    Statement statement = Mockito.mock(Statement.class);
    Mockito.when(statement.executeQuery(Mockito.anyString())).thenReturn(resultSet);

    Mockito.when(mockConnection.createStatement()).thenReturn(statement);

    citiesServlet.doGet(testRequest, testResponse);
}

}


回答1:


Update for anyone who lands on this. I found out that this is a regression bug with PowerMock 1.6.6 and there is a bug(#177) filed in the github repo of PowerMock(https://github.com/powermock/powermock/issues/717) with a target fix version of 1.6.7. Moving back to 1.6.5 fixed the issue for me.



来源:https://stackoverflow.com/questions/40964605/org-powermock-reflect-exceptions-methodnotfoundexception-when-mocking-java-sq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!