在JUNIT测试中没有创建Hibernate Transaction



最初,在前端更改时,数据并未立即归于数据库。交易从未创建和投入,这就是为什么我将交易部分添加到代码中的原因。它现在可以很好地工作,但是当我在编写交易部分后测试相同的方法时,测试用例的第一个失败抛出NPE(null指针异常),而其余的使用相同方法通过的测试用例。我是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方法中在Trassaction.commit()上抛出Null指针异常。当我调试测试案例时,我会发现交易具有无效值。不确定为什么交易不会创建。第二个测试用例运行良好。

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(Console)

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 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>

如果我使用捕获块来捕获空指针异常,则测试用例通过。我不确定为什么我必须抓住根本没有被抛出的例外,W.R.T其他测试用例。

相关内容

  • 没有找到相关文章

最新更新