【问题标题】:Manage Exceptions in Junit在 Junit 中管理异常
【发布时间】:2016-03-15 14:04:07
【问题描述】:

我有一个应用程序(使用注释的 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'

【问题讨论】:

  • 因为是执行时间异常,可能你需要使用其他不同的异常类型,如通用异常或RuntimeException,你的方法中也不需要断言,只需抛出异常
  • 您作为测试执行的方法没有出现在异常的堆栈跟踪中。异常是否可能发生在不期望任何异常的不同测试中?
  • @cralfaro 它发生在上面的测试 testInsertOrUpdateProductCriteria2 方法行 57

标签: java spring hibernate spring-mvc junit


【解决方案1】:

您的测试将无法运行,因为在引发异常时测试会停止。在这种情况下无法到达Assert.isTrue(test2miseria)

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-07-11
      • 2013-05-11
      • 2016-04-13
      • 2011-09-26
      • 2016-02-02
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多