Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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 捕获异常后代码失败_Java_Hibernate_Spring Boot_Junit - Fatal编程技术网

Java 捕获异常后代码失败

Java 捕获异常后代码失败,java,hibernate,spring-boot,junit,Java,Hibernate,Spring Boot,Junit,我正在为我的服务类编写一个单元测试。 我还想测试无法工作的案例,然后捕获异常并继续。但是如果我尝试这个,测试总是失败 @Before public void intial() { a = new Article(); a.setName("stock test article"); articleService.save(a); create = new StorageLocation(); create.setCarlaStorageId(-46

我正在为我的服务类编写一个单元测试。 我还想测试无法工作的案例,然后捕获异常并继续。但是如果我尝试这个,测试总是失败

    @Before
public void intial() {
    a = new Article();
    a.setName("stock test article");
    articleService.save(a);
    create = new StorageLocation();
    create.setCarlaStorageId(-4636L);
    storageLocationService.save(create);
}

/**
 * Test of save method, of class StockService.
 */
@Test
public void testSave() {
    System.out.println("save");
    Stock save = new Stock();

    // if i do this the test fails, but why ?
//        try {
//            instance.save(save);
//            fail("saved stock without article");
//        } catch (Exception e) {
//            // correct
//            System.out.println("unable to save without article.");
//        }

    save.setArticle(a);
    save.setCntStock(6);
    save.setOldStock(2);

    // if i do this the test fails, but why ?
//        
//        try {
//            instance.save(save);
//            fail("saved stock without storagelocation");
//        } catch (Exception e) {
//            // correct
//            System.out.println("unable to save without storagelocation.");
//        }

    save.setStorageLocation(this.create);
    save.setLastModify(new Timestamp(System.currentTimeMillis()));
    instance.save(save);
}

@After
public void cleanUp() {
    this.storageLocationService.delteLocation(create);
}
保存方法

    public void save(final Stock save) {

    try (StatelessSession session = this.hibernateFactory.openStatelessSession()) {
        Transaction beginTransaction = session.beginTransaction();
        beginTransaction.begin();
        if (save.getId() != null) {
            session.update(save);
        } else {
            session.insert(save);
        }
        save.getArticle().setEdited(true);
        session.update(save.getArticle());
        beginTransaction.commit();
    } catch (Exception e) {
        save.setId(null);
        throw e;
    }
}
错误消息:

org.hibernate.exception.genericjdbception:无法提取结果集 位于org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182) 位于org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911) 位于org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173) 位于org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:645) 位于org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:495) 位于org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:380) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:498) 位于org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(statementfacom.java:114) 位于com.sun.proxy.$Proxy126.executeQuery(未知源) 位于org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70) 位于org.hibernate.id.SequenceGenerator.GenerateHold(SequenceGenerator.java:116) 位于org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:109) 在org.hibernate.internal.StatelessSessionImpl.insert(StatelessSessionImpl.java:145)上 在org.hibernate.internal.StatelessSessionImpl.insert(StatelessSessionImpl.java:138)上 在com.mm.inventur.services.StockService.save上(StockService.java:78) 在com.mm.inventur.services.StockService$$FastClassBySpringCGLIB$$75cab27c.invoke()上 位于org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 位于org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669) 在com.mm.inventur.services.StockService$$EnhancerBySpringCGLIB$$5bf3b5b6.save()上 位于com.mm.inventur.services.StockServiceTest.testSave(StockServiceTest.java:84)


testSave(com.mm.inventur.services.StockServiceTest)运行时间:0.126秒执行
instance.save(保存)操作执行?根据您发布的内容,我们所能说的是,测试很可能失败,因为instance.save()应该引发异常,但没有。现在添加了save方法的代码。确定。那么调试代码时会发生什么呢。正在执行哪些行?“没有错误消息,只是关于try-catch中的代码失败原因的消息。”这毫无意义。当测试失败时,您会出现错误(抛出异常)或断言失败。请准确提供您收到的JUnit消息。