gpt4 book ai didi

java - 如何模拟 DriverManager.getConnection(...)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:50:19 26 4
gpt4 key购买 nike

我有一个类,它连接到 H2 数据库并运行多个 SQL 语句。

public class H2Persistence implements IPersistence {

private Connection conn;

@Override
public void open() {
try
{
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection(CONN_TYPE_USER_HOME);

final Statement stmt = conn.createStatement();

stmt.executeUpdate("CREATE TABLE PERSON(" +
"ID BIGINT,"+
"AGEGROUP VARCHAR(255),"+
"MONTHLY_INCOME_LEVEL VARCHAR(255)," +
"GENDER VARCHAR(1),"+
"HOUSEHOLD_ID BIGINT)");

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
...
}

我想编写一个单元测试,验证在 open 方法中是否执行了某个 SQL 语句(DROP TABLE IF EXISTS PERSON)。

为了做到这一点,我编写了以下测试:

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;


@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class)
public class H2PersistenceTest {
@Test
public void testDropPersonIsCalled() throws SQLException {
final Statement statement = mock(Statement.class);

final Connection connection = mock(Connection.class);

when(connection.createStatement()).thenReturn(statement);

mockStatic(DriverManager.class);

when(DriverManager.getConnection(H2Persistence.CONN_TYPE_USER_HOME)).thenReturn
(connection);


final H2Persistence objectUnderTest = new H2Persistence();

objectUnderTest.open();
verify(statement.executeUpdate("DROP TABLE IF EXISTS PERSON"));
}
}

但它不起作用 - DriverManager 返回真实连接,而不是模拟连接。

我该如何修复它(让 DriverManager 在测试中返回连接模拟)?

这是我项目的pom.xml,可能那里有问题。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.mycompany</groupId>
<artifactId>myproduct</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<powermock.version>1.5.1</powermock.version>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-util</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<version>2.0M8</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.173</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>

最佳答案

这个有效(注意导入):

import static org.easymock.EasyMock.expect;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.api.easymock.PowerMock.replay;


@RunWith(PowerMockRunner.class)
@PrepareForTest({DriverManager.class, H2Persistence.class})
public class H2PersistenceTest {
@Test
public void testDropPersonIsCalled() throws SQLException {
final Statement statement = mock(Statement.class);

final Connection connection = mock(Connection.class);

when(connection.createStatement()).thenReturn(statement);

mockStatic(DriverManager.class);

expect(DriverManager.getConnection(H2Persistence.CONN_TYPE_USER_HOME))
.andReturn(connection);
expect(DriverManager.getConnection(null))
.andReturn(null);

replay(DriverManager.class);
final H2Persistence objectUnderTest = new H2Persistence();

objectUnderTest.open();

verify(statement).executeUpdate("DROP TABLE IF EXISTS PERSON");
verify(statement).executeUpdate(H2Persistence.CREATE_TABLE_PERSON);
}
}

关于java - 如何模拟 DriverManager.getConnection(...)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19464975/

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