Mockito数据源测试在调试模式下失败

时间:2018-04-16 22:40:13

标签: java mockito testng datasource resultset

为什么在使用行

处的断点在调试模式下运行时,此单元测试失败

while(rs.next()){

尽管测试中有指令,它仍将在第一次迭代后退出循环。

Mockito.when(mockResultSet.next())。thenReturn(true,true,false);

在正常模式下运行时,此单元测试工作正常。

注意:我使用的是Intellij,testNG。

public class ProjectDao {
private static Logger LOG = LogManager.getLogger(ProjectDao.class);
private DataSource dataSource;

private int id;
private String name;

// For Testing
public ProjectDao(DataSource dataSource) {
    this.dataSource = dataSource;
}

public ProjectDao() {
    dataSource = Database.getDataSource();
}
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
public List<Project> getProjects() throws Exception {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    String SQL = "SELECT project.id, project.name FROM cicd.project";
    List<Project> projects = new ArrayList<>();
    try {
        conn = dataSource.getConnection();
        ps = conn.prepareStatement(SQL);
        rs = ps.executeQuery();



        while (rs.next()) {
            Project p = new Project();
            p.setId(rs.getInt("id"));
            p.setName(rs.getString("name"));

            projects.add(p);
        }

        LOG.debug("found: " + projects);
        return projects;

    } catch (SQLException e) {
        LOG.error("Error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                LOG.error("Error closing resultset: " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                LOG.error("Error closing PreparedStatement: " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                LOG.error("Error closing connection: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    return projects;
}

测试类

@PrepareForTest(ProjectDao.class)
public class ProjectDaoTests {
private static Logger LOG = LogManager.getLogger(ProjectDaoTests.class);

List<Project> projects;
Project projectA = new Project();
Project projectB = new Project();

@Mock
DataSource mockDataSource;

@Mock
Connection mockConn;

@Mock
PreparedStatement mockPreparedStmnt;

@Mock
ResultSet mockResultSet;

@BeforeTest(groups = "ut")
public void initMocks() throws IOException, SQLException {
    MockitoAnnotations.initMocks(this);


    when(mockDataSource.getConnection()).thenReturn(mockConn);
    when(mockDataSource.getConnection(anyString(), anyString())).thenReturn(mockConn);
    doNothing().when(mockConn).commit();
    when(mockConn.prepareStatement(anyString())).thenReturn(mockPreparedStmnt);
    when(mockPreparedStmnt.executeQuery()).thenReturn(mockResultSet);

    projects = new ArrayList<>();
} 

@Test(groups = "ut")
public void testProjects() throws Exception {
    projectA.setId(1);
    projectA.setName("projectA");
    projectB.setId(2);
    projectB.setName("projectB");
    projects.add(projectA);
    projects.add(projectB);

    Mockito.when(mockResultSet.next()).thenReturn(true, true, false);

    Mockito.when(mockResultSet.getInt("id")).thenReturn(projectA.getId(), projectB.getId());
    Mockito.when(mockResultSet.getString("name")).thenReturn(projectA.getName(), projectB.getName());

    ProjectDao projectDao = new ProjectDao(mockDataSource);
    List<Project> projectsList = projectDao.getProjects();

    Assert.assertEquals(projectsList.size(), 2);
    Assert.assertEquals(projectsList.get(0).getId(), projectA.getId());
    Assert.assertEquals(projectsList.get(0).getName(), projectA.getName());
    Assert.assertEquals(projectsList.get(1).getId(), projectB.getId());
    Assert.assertEquals(projectsList.get(1).getName(), projectB.getName());
}

0 个答案:

没有答案