gpt4 book ai didi

java - Mockito 模拟数据库调用

转载 作者:行者123 更新时间:2023-12-02 00:08:16 27 4
gpt4 key购买 nike

我有一个类,它接受数据库 url 和用户名,连接到数据库,并执行查询,将结果分配给 HashMap 。

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());
}
}
}

我正在尝试使用 Mockito 来模拟 queryDatabase()。我添加了 PowerMockito 来模拟 DriverManager

我的测试类:

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);
}
}

运行测试的输出是:

java.sql.SQLException: No suitable driver found for test_url
{}

我期望:

{1=john smith}

我怎样才能正确地模拟这个?

除非绝对必要,否则我已经阅读过避免使用 PowerMock 的内容,因此我也非常愿意接受重构的建议,以便尽可能避免。

最佳答案

您可以尝试执行以下操作:

class MyClassTest extends PowerMockTestCase {

添加依赖项:

    <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>

您可能还需要在设置中使用 initMock:

@Before
public void setUp() throws Exception {
mockStatic(DriverManager.class);
initMocks(this);

关于java - Mockito 模拟数据库调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58141574/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com