Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Junit中管理异常_Java_Spring_Hibernate_Spring Mvc_Junit - Fatal编程技术网

Java 在Junit中管理异常

Java 在Junit中管理异常,java,spring,hibernate,spring-mvc,junit,Java,Spring,Hibernate,Spring Mvc,Junit,我有一个应用程序(Spring4MVC+Hibernate4+MySQL+Maven集成示例使用注释),使用基于注释的配置将Spring与Hibernate集成。我有一个junit: @Test (expected = org.hibernate.exception.GenericJDBCException.class) public void testInsertOrUpdateProductmiseria2() { List<ProductGroup>

我有一个应用程序(Spring4MVC+Hibernate4+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);

    }

引发的异常是
java.sql.BatchUpdateException
,您期望的是
org.hibernate.exception.genericjdbception

,因为引发异常时测试将停止。在这种情况下,
Assert.isTrue(test2miseria)


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

由于执行时存在异常,可能需要使用其他不同的异常类型,如泛型异常或RuntimeException,而且方法中不需要断言,只需抛出异常您作为测试执行的方法不会出现在异常的堆栈跟踪中。异常是否可能发生在不期望任何异常的其他测试中?@cralfaro它发生在上述测试testInsertOrUpdateProductCriteria2方法第57行
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'