简体   繁体   English

JUNIT 测试中未创建 Hibernate 事务

[英]Hibernate Transaction is not getting created in JUNIT tests

Initially, the data was not being committed to the database immediately upon the change in the frontend.最初,数据并未在前端发生更改后立即提交到数据库。 The transaction was never created and committed, which is why I have added the transaction part to the code.事务从未创建和提交,这就是我将事务部分添加到代码中的原因。 It works well now but when I test the same method after writing the transaction part one of the test cases fail throwing NPE( null pointer exception) while the remaining test cases which use the same method pass.它现在运行良好,但是当我在编写事务部分后测试相同的方法时,其中一个测试用例失败抛出 NPE(空指针异常),而使用相同方法的其余测试用例通过。 I am a Rookie at Spring Hibernate, Trying to understand the reason behind the failure.我是 Spring Hibernate 的新手,试图了解失败背后的原因。

DummyDAO.java 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();
        }
    }

In the below-mentioned test cases, the first test case fails throwing a null pointer exception at transaction.commit() in the dao method.在下面提到的测试用例中,第一个测试用例在 dao 方法中的 transaction.commit() 处抛出空指针异常失败。 When I debug the test case, I see that the transaction has a null value.当我调试测试用例时,我看到事务有一个空值。 Not sure why the transaction is not getting created.不知道为什么交易没有被创建。 The second Test case runs well.第二个测试用例运行良好。

DummyDAOTest.java 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:询问:

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

If I use a catch block to catch the Null Pointer Exception then the test cases passes.如果我使用 catch 块来捕获空指针异常,则测试用例通过。 I am not sure why would I have to catch the exception which is not being thrown at all, wrt other test cases.我不确定为什么我必须捕获根本没有抛出的异常,而不是其他测试用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM