需要说明事前冲洗的必要性,以避免在春季测试时出现误报? [英] Need explanation on the necessity of a prior flushing to avoid false positives whenTesting with Spring ?

查看:82
本文介绍了需要说明事前冲洗的必要性,以避免在春季测试时出现误报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于测试的春季文档,它指出:


在测试ORM
代码时避免误报



当您测试涉及
ORM框架(如JPA或
Hibernate)的代码时,请在
更新的测试方法中刷新基础
会话会议的状态。
无法刷新ORM框架的
底层会话可能会产生错误的
正数:您的测试可能会通过,但
相同的代码会在
实时生产中抛出异常环境。在基于Hibernate的示例测试
情况下的
中,一个方法展示了一个错误的
正数,另一个方法
正确地显示了
刷新会话的结果。 / p>

有人可以解释为什么我需要调用flush吗?

解决方案

好的,你实际上跳过了有趣的部分,例子:)这是:

  // ... 

@Autowired
private SessionFactory sessionFactory;

@Test //无预期异常!
public void falsePositive(){
updateEntityInHibernateSession();
//假阳性:一旦会话为
//最后刷新(即在产品代码中)
}

@Test(预期= GenericJDBCException.class)
public void updateWithSessionFlush(){
updateEntityInHibernateSession();
//需要手动刷新以避免在测试中出现误报
sessionFactory.getCurrentSession()。flush();
}

// ...

这个例子试图说明的是,除非实际上 flush 会话(也称为第一级缓存)与数据库同步内存中的更改,否则您并未真正测试数据库集成并可能不会测试真正的预期行为或错过一个问题。例如,数据库可能会返回一个错误,因为违反了约束条件,如果您不' t命中数据库,就不会出现这种正确的行为,如上面的 falsePositive()测试方法。这种测试方法应该失败,或者期望有一个例外,但只会通过。另一方面,其他测试方法与冲洗测试真实的行为。因此需要 flush


In the spring documentation regarding testing, it states:

Avoid false positives when testing ORM code

When you test code involving an ORM framework such as JPA or Hibernate, flush the underlying session within test methods which update the state of the session. Failing to flush the ORM framework's underlying session can produce false positives: your test may pass, but the same code throws an exception in a live, production environment. In the following Hibernate-based example test case, one method demonstrates a false positive and the other method correctly exposes the results of flushing the session.

Can someone explain why i need to call flush?

解决方案

Well, you actually skipped the interesting part, the example :) Here it is:

// ...

@Autowired
private SessionFactory sessionFactory;

@Test // no expected exception!
public void falsePositive() {
    updateEntityInHibernateSession();
    // False positive: an exception will be thrown once the session is
    // finally flushed (i.e., in production code)
}

@Test(expected = GenericJDBCException.class)
public void updateWithSessionFlush() {
    updateEntityInHibernateSession();
    // Manual flush is required to avoid false positive in test
    sessionFactory.getCurrentSession().flush();
}

// ...

What this example tries to illustrate is that unless you actually flush the session (A.K.A. the first level cache) to sync in-memory changes with the database, you're not really testing the database integration and might not test the real expected behavior or miss a problem.

For example, the database could return an error because of, say a constraint violation, and if you don't hit the database, you won't exhibit this right behavior, as in the falsePositive() test method above. This test method should fail, or expect an exception but will just pass. On the other hand, the other test method with the flush does test the real behavior. Hence the need to flush.

这篇关于需要说明事前冲洗的必要性,以避免在春季测试时出现误报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆