I have a class that takes in a database url and username, connects to the database, and executes a query, assigning the results to a hash map.
import java.sql.*;
import java.util.Map;
public class MyClass {
private final String dbUrl;
private final String username;
MyClass(final String dbUrl, final String username) {
this.dbUrl = dbUrl;
this.username = username;
}
public void queryDatabase(final Map<Integer, String> userIdToName) {
final String query = "select user_id, name from users";
try (final Connection connection = DriverManager.getConnection(dbUrl, username, null);
final PreparedStatement ps = connection.prepareStatement(query);
final ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
final int userId = rs.getInt(1);
final String name = rs.getString(2);
userIdToName.put(userId, name);
}
} catch (final SQLException e) {
System.out.println(e.toString());
}
}
}
I am trying to use Mockito to mock queryDatabase()
. I added PowerMockito to mock DriverManager
.
My test class:
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class)
class MyClassTest {
@Mock private Connection mockConnection = mock(Connection.class);
@Mock private PreparedStatement mockStatement = mock(PreparedStatement.class);
@Mock private ResultSet resultSet = mock(ResultSet.class);
@InjectMocks private static MyClass myClass = new MyClass("test_url", "test_username");
@Before
public void setUp() throws Exception {
mockStatic(DriverManager.class);
when(DriverManager.getConnection(anyString(), anyString(), null)).thenReturn(mockConnection);
}
@Test
void testQueryDatabase() throws Exception {
when(mockConnection.prepareStatement(anyString())).thenReturn(mockStatement);
when(mockStatement.executeQuery(anyString())).thenReturn(resultSet);
when(resultSet.getInt(Mockito.anyInt())).thenReturn(1);
when(resultSet.getString(anyInt())).thenReturn("john smith");
final Map<Integer, String> useridToUser = new HashMap<>();
myClass.queryDatabase(useridToUser);
System.out.println(useridToUser);
}
}
The output of running my test is:
java.sql.SQLException: No suitable driver found for test_url
{}
I expect:
{1=john smith}
How can I properly mock this?
I have read to avoid PowerMock
unless absolutely necessary, so I am also very open to suggestions to refactor to avoid if possible.
You could try to do following:
class MyClassTest extends PowerMockTestCase {
Add dependencies:
<dependency>
<groupId>org.powermock.tests</groupId>
<artifactId>powermock-tests-utils</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>1.4.9</version>
<scope>test</scope>
</dependency>
Also probably you need initMock in setup:
@Before
public void setUp() throws Exception {
mockStatic(DriverManager.class);
initMocks(this);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.