虽然引发了预期的异常,但JUnit测试失败了

时间:2018-06-05 21:53:54

标签: java unit-testing junit mockito

在标题上说,即时测试DAO函数,我希望函数抛出一个SQLException。当我运行测试时,我得到以下stracktrace:

Jun 05, 2018 11:49:55 PM org.han.ica.oose.sneeuwklokje.database.admin.AdminDaoImpl getElections
SEVERE: Cant get elections. 
java.sql.SQLException
    at org.han.ica.oose.sneeuwklokje.database.admin.AdminDaoImpl.getElections(AdminDaoImpl.java:29)
    at org.han.ica.oose.sneeuwklokje.database.admin.AdminDaoImplTest.testGetElectionsWithException(AdminDaoImplTest.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


java.lang.AssertionError: Expected exception: java.sql.SQLException

    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

这是测试:

@Test(expected = SQLException.class)
public void testGetElectionsWithException() throws SQLException {

    try {
        when(mockConn.prepareStatement(anyString())).thenThrow(new SQLException());
        adminDao.getElections('>');
    } catch (SQLException e) {
        //verify and assert
        verify(mockConn, times(1)).prepareStatement(anyString());
        verify(mockPreparedStmnt, times(0)).executeQuery();
        verify(mockResultSet, times(0)).next();
        verify(mockResultSet, times(0)).getInt("id");
        verify(mockResultSet, times(0)).getString("name");
        verify(mockResultSet, times(0)).getDate("startDate");
        verify(mockResultSet, times(0)).getDate("endDate");
        throw e;
    }
}

这是DAO功能:

@Override
public ElectionListResponse getElections(char selector) {
    ElectionListResponse electionListResponse = new ElectionListResponse();

    String query = NamedQueries.GET_ELECTIONS_BEGIN + selector + NamedQueries.GET_ELECTIONS_END;

    try {
        con = sqlConnection.getConnection();
        stmt = con.prepareStatement(query);
        rs = stmt.executeQuery();

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        while (rs.next()) {
            Election election = new Election();
            election.setElectionID(rs.getInt("id"));
            election.setElectionName(rs.getString("name"));
            election.setStartDate(df.format(rs.getDate("startDate")));
            election.setEndDate(df.format(rs.getDate("endDate")));
            electionListResponse.addElection(election);
        }
        return electionListResponse;
    } catch (SQLException e) {
        LOGGER.log(Level.SEVERE, "Cant get elections. ", e);
    } finally {
        closeConnection();
    }
    return null;
}

我似乎无法弄清楚为什么这个测试失败了。知道为什么失败了吗?如果可能,您可以发布代码实际应该如何测试?

2 个答案:

答案 0 :(得分:1)

您记录异常但随后返回null。所以测试永远不会看到异常

答案 1 :(得分:1)

您没有从getElections()中抛出任何异常。

修改代码以抛出异常,如下所示,

修改后的代码:

try {
    /* ... */
    return electionListResponse;
} catch (SQLException e) {
    LOGGER.log(Level.SEVERE, "Cant get elections. ", e);
    throw e;
} finally {
    closeConnection();
}
return null;