How mock internal method call with EasyMock and PowerMock?

I have JDBC connection class as below. Need to mock the Connection class with Easy Mock and Power Mock. It is throwing below error.I’m new to unit testing. Please give some suggestion to fix this issue. If I remove the connection.isClosed() method test is passing.

public class ConnectionFactory {

    private static volatile Connection connection        = null;
    private ConnectionFactory() {

    public static Connection getConnection() {   
        try {

            if (connection == null || connection.isClosed()) {

                try {     
                            connection = DriverManager.getConnection(DB_CONNECTION_URL, DB_USER, DB_PASSWORD);


I have mock the connection as below.

public class ConnectionFactoryTest {

    public void testGetConnection() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {

        Connection mockConnection = PowerMock.createMock(Connection.class);
        Field connectionField = ConnectionFactory.class.getDeclaredField("connection");
        connectionField.set(null, mockConnection);





  Unexpected method call Connection.isClosed():
    at org.easymock.internal.MockInvocationHandler.invoke(
    at org.easymock.internal.ObjectMethodsFilter.invoke(

Source: java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.