gpt4 book ai didi

java - 使用mockito测试DriverManager.getConnection

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:42 29 4
gpt4 key购买 nike

我编写了 DAO 类

public List<ClassRoom> getAllClassRooms() {
List<ClassRoom> classRoomsList = new ArrayList<>();
String sqlStatement = "SELECT * FROM university.classrooms;";
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/Exam6", "postgres", "Navlanart1");
try {
statement = connection.prepareStatement(sqlStatement);
try {
resultSet = statement.executeQuery();
while (resultSet.next()) {
ClassRoom classRoom = new ClassRoom(resultSet.getString("name"));
classRoomsList.add(classRoom);
}
} catch (SQLException e) {
} finally {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return classRoomsList;
}

现在我想通过类来测试它

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.easymock.PowerMock.mockStatic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;

import com.kolosok.university.dao.ClassRoomDao;

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(DriverManager.class)
public class TestClassRoomDao {

@Mock
Connection mockConnection;

@Mock
PreparedStatement mockPreparedStatement;

@Mock
ResultSet mockResultSet;

@Test
public void testGetAllClassRooms() throws SQLException {

mockStatic(DriverManager.class);

when(DriverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(mockConnection);
when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(Boolean.FALSE);

ClassRoomDao classRoomDao = new ClassRoomDao();
classRoomDao.getAllClassRooms();

verify(mockConnection, times(1)).prepareStatement(anyString());
verify(mockPreparedStatement, times(1)).executeQuery();
verify(mockResultSet, times(1)).next();
}

}

但是它不起作用。

java.sql.SQLException: No suitable driver found for 
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.kolosok.university.TestClassRoomDao.testGetAllClassRooms(TestClassRoomDao.java:58)

我阅读了很多关于类似问题的 stackOverFlow 答案。但还是无法解决我的问题

我正在使用

postgresql-42.0.0.jre7.jar

mockito-all-1.10.19.jar

powermock-api-easymock-1.6.6.jar

powermock-core-1.6.6.jar

cglib-nodep-3.1.jar

javassist-3.12.1.GA.jar

objenesis-2.5.jar

powermock-api-mockito-1.6.6.jar

powermock-api-support-1.6.6.jar

easymock-3.4.jar

最佳答案

我认为问题可能是您没有使用 PowerMockRunner 运行它,因此不遵守正在使用的 PowerMock 扩展(如 mockStatic )

尝试将@RunWith(MockitoJUnitRunner.class)更改为@RunWith(PowerMockRunner.class)

编辑

您可能还需要初始化模拟。最简单的方法是使用 @Before 方法,例如

@之前
公共(public)无效设置(){
initMocks(这个);
}

关于java - 使用mockito测试DriverManager.getConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43095958/

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