我正在为我的服务等级编写单元测试。 我还想测试无法工作的案例,然后捕获异常并继续。但是,如果我尝试此操作,则测试始终会失败。
@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.GenericJDBCException:无法提取ResultSet 在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(StatementFacacom.java:114) com.sun.proxy。$ Proxy126.executeQuery(未知来源) 在org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70) 在org.hibernate.id.SequenceGenerator.generateHolder(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秒<<<错误! org.springframework.orm.jpa.JpaSystemException:无法执行语句;嵌套的异常是org.hibernate.exception.GenericJDBCException:无法执行语句 在org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
感谢您的帮助。