管理Junit中的例外情况

时间:2016-03-15 14:04:07

标签: java spring hibernate spring-mvc junit

我有一个应用程序(使用注释的Spring 4 MVC + Hibernate 4 + MySQL + Maven集成示例),使用基于注释的配置将Spring与Hibernate集成。我有这个junit:

@Test (expected = org.hibernate.exception.GenericJDBCException.class)
    public void testInsertOrUpdateProductmiseria2() {

        List<ProductGroup> allProductGroups = productGroupDao.findAll();

        boolean test2miseria = true;

        for (ProductGroup productGroup : allProductGroups) {
            List<Productmiseria> productmiseria = miseriaDao.getmiseriaForProductGroup (productGroup.getId());

            if (productmiseria.size()==2) {


                Productmiseria miseria = new Productmiseria();
                miseria.setAdoptionDate(new Date());
                miseria.setExpirationDate(new Date());
                miseria.setCode("code");
                miseria.setProductGroup(productGroup);

                miseriaDao.saveOrUpdatemiseria(miseria);

                // can't reach this line. An exception has to be throwed by the TRIGGER TRG_miseria_ROWS  
                //ORA-20200: Product Group can not have more than 2 miserias
                test2miseria=false;

            }
        }
        Assert.isTrue(test2miseria);

    }

我也试过 @Test(expected = java.sql.BatchUpdateException.class)

但即使抛出异常,测试也未通过

ORA-06512: at "DEVICES.TRG_CRITERIA_ROWS", line 6
ORA-04088: error during execution of trigger 'DEVICES.TRG_CRITERIA_ROWS'

(def.AbstractFlushingEventListener   301 ) Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:65)
    at com.tdk.env.devicelabel.dao.impl.CriteriaDaoImpl.getCriteriaForProductGroup(CriteriaDaoImpl.java:59)
    at com.tdk.env.devicelabel.dao.CriteriaDaoTest.testInsertOrUpdateProductCriteria2(CriteriaDaoTest.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:79)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:74)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:179)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:287)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:258)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:176)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.sql.BatchUpdateException: ORA-20200: Product Group can not have more than 2 criterias
ORA-06512: at "DEVICES.TRG_CRITERIA_ROWS", line 6
ORA-04088: error during execution of trigger 'DEVICES.TRG_CRITERIA_ROWS'

2 个答案:

答案 0 :(得分:0)

抛出的异常为java.sql.BatchUpdateException,您期待org.hibernate.exception.GenericJDBCException

答案 1 :(得分:0)

您的测试无效,因为测试在抛出异常时停止。在这种情况下,未达到Assert.isTrue(test2miseria)

测试方法是与任何其他方法一样执行的标准方法。例如。他们停在抛出异常的地方。唯一的区别是JUnit调用此方法,如果异常属于预期类型,则不会报告测试错误。