我写了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方法,例如

@Beforepublic void setup(){ initMocks(this);}

09-13 04:35