'Mockito Inline only covers some static methods when called

I'm using Mokito inline to test some static calls, however some calls have no coverage despite no errors.

This is one of the methods I want to mock

    public static UserInfo getUserInformations(int id) {
    String QUERY = "SELECT * FROM applications WHERE id = ?";

    PreparedStatement preparedStatement;

    try {
        preparedStatement = ConnectionManager.getDBConnection().prepareStatement(QUERY);

        preparedStatement.setInt(1, id);

        ResultSet rs = preparedStatement.executeQuery();

        if (rs.next()) {
            return new UserInfo(rs.getInt("id"), rs.getInt("ownerid"), rs.getString("name"),
                    rs.getString("description"), rs.getString("website"), rs.getInt("legion"),
                    rs.getString("apikey"));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

This is the test class

public class ListUserInfoServletTest {
        private static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
        private static final HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
        private static final HttpSession session = Mockito.mock(HttpSession.class);
        private static final RequestDispatcher req = Mockito.mock(RequestDispatcher.class);
        private static final ListApplicationServlet servlet = new ListApplicationServlet();
        private static final MockedStatic<UserDAO> mockUserDAO = Mockito.mockStatic(UserDAO.class);
        
        @Test
        public void ListUserInfoServletTest()
        {
            Mockito.doReturn(session).when(request).getSession(); 
            Mockito.doReturn(req).when(request).getRequestDispatcher(Mockito.anyString());   
            UserInfo userInfo = new UserInfo(1, 2, "testuser", "description", "http://example.com", "example", "testkey");
            
            mockUserDAO.when(() -> UserDAO.getUserInformations(1)).thenReturn(userInfo);
             
            //Asserts and so on down here, cutted.
            mockUserDAO.close();
        }
}

When I run the test it totally ignores the mock showing 0% coverage on the getUserInformations() method. I have other methods in that class that are equal to this getter and are beign called and marked as covered. I'm using mockito inline in the wrong way?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source