1

My goal is to mock the static method

DriverManager.getConnection(String, String, String)

using Mockito and PowerMock.

The code I am executing is the following:

import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.sql.DriverManager;
import java.sql.SQLException;

import static org.mockito.Matchers.any;
import static org.mockito.BDDMockito.given;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest({DriverManager.class})
public class SqlHelperConnectionTest {

    @org.junit.Test
    public void testSqlConnectionSQLException() throws Exception {

        mockStatic(DriverManager.class);
        given(DriverManager.getConnection(any(String.class), any(String.class), any(String.class))).willThrow(new SQLException("ERROR test"));

        DriverManager.getConnection("","","");
    }
}

The error I get is the following:

java.lang.ClassCastException: org.mockito.internal.matchers.LocalizedMatcher cannot be cast to org.mockito.ArgumentMatcher

The dependencies I am using are the following:

testCompile group: 'junit', name: 'junit', version: '4.10'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.powermock:powermock-module-junit4:1.6.4'
testCompile 'org.powermock:powermock-api-mockito:1.6.4'

The versions I am using are those described on this post.

I would appreciate any hint or suggestion.

Thanks a lot in advance!

pafede2
  • 1,626
  • 4
  • 23
  • 40

0 Answers0