在JUNIT测试中没有创建Hibernate Transaction

时间:2017-12-25 17:50:12

标签: java spring hibernate junit mockito

最初,在前端发生更改后,数据未立即提交到数据库。事务从未创建和提交,这就是我将事务部分添加到代码中的原因。它现在运行良好,但是当我在编写事务部分之后测试相同的方法时,其中一个测试用例在抛出NPE(空指针异常)时失败,而使用相同方法的其余测试用例则通过。我是Spring Hibernate的新手,试图了解失败背后的原因。

DummyDAO.java

public void remove(final int roleId, final int round, final int gameId)
    {
        Preconditions.checkArgument(roleId > 0, INVALID_ROLEID);
        Preconditions.checkArgument(round > 0, INVALID_ROUND);
        Preconditions.checkArgument(gameId > 0, INVALID_GAMEID);
        Session session = factory.openSession();
        final Transaction transaction = session.beginTransaction();
        try
        {
            Query query = session.getNamedQuery(REMOVE);
            query.setParameter(ROLE_ID, roleId);
            query.setParameter(ROUND, round);
            query.setParameter(GAME_ID, gameId);
            query.executeUpdate();
            transaction.commit();
        }
        finally
        {
            session.close();
        }
    }

在下面提到的测试用例中,第一个测试用例无法在dao方法的transaction.commit()中抛出空指针异常。当我调试测试用例时,我发现事务的值为空。不确定为什么没有创建交易。第二个测试用例运行良好。

DummyDAOTest.java

@Test
    public void remove_getNamedQueryValidName_NoError()
    {
        final ArgumentCaptor<String> argumentCaptor = ArgumentCaptor
                .forClass(String.class);
        roleQuestionRoundDAO.remove(ROLE_ID_ONE, ROUND_ONE, GAME_ID_ONE);
        verify(session).getNamedQuery(argumentCaptor.capture());
        assertEquals(RoleQuestionRoundDAO.REMOVE, argumentCaptor.getValue());
    }

@Test
    public void remove_roleId_SetParameterFails()
    {
        doThrow(HibernateException.class).when(queryMocked).setParameter(eq("roleId"), eq(ROLE_ID_ONE));
        expectedException.expect(HibernateException.class);
        roleQuestionRoundDAO.remove(ROLE_ID_ONE, ROUND_ONE, GAME_ID_ONE);
    }

StackTrace(控制台)

java.lang.NullPointerException
    at dummy.model.RoleQuestionRoundDAO.remove(RoleQuestionRoundDAO.java:288)
    at dummy.model.RoleQuestionRoundDAOTest.remove_getNamedQueryValidName_NoError(RoleQuestionRoundDAOTest.java:790)
    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.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    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.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)

QUERY:

<query name="remove">
        <![CDATA[update RoleQuestionRound rq set rq.activeInd = 0 where rq.roleQuestionRoundComposite.round = :round and rq.roleQuestionRoundComposite.roleId = :roleId and rq.roleQuestionRoundComposite.gameId = :gameId]]>
    </query>

1 个答案:

答案 0 :(得分:1)

如果我使用catch块来捕获Null Pointer Exception,那么测试用例就会通过。我不知道为什么我必须抓住没有被抛出的异常,w.r.t其他测试用例。