使用mockito

时间:2017-03-29 14:09:02

标签: java junit mockito powermock easymock

我写过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)

我在类似的quastions上读了很多stackOverFlow答案。但仍然无法解决我的问题

我正在使用

的PostgreSQL-42.0.0.jre7.jar

的Mockito-全1.10.19.jar

powermock-API-EasyMock的-1.6.6.jar

powermock核-1.6.6.jar

CGLIB的节点p-3.1.jar

了Javassist-3.12.1.GA.jar

objenesis-2.5.jar

powermock-API的Mockito-1.6.6.jar

powermock-API支持-1.6.6.jar

EasyMock的-3.4.jar

1 个答案:

答案 0 :(得分:0)

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

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

修改

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

@Before public void setup(){ initMocks(this); }