简体   繁体   English

用jmockit模拟扩展类的抽象方法

[英]mock abstract method of extended class with jmockit

I have a method inside my Dao class like this: 我的Dao类中有一个这样的方法:

  @Override
  public List<Dog> loadAllDog(Date pDate) {
    final MapSqlParameterSource lParameterSource = new MapSqlParameterSource();
    lParameterSource.addValue("jdate", pDate);

    final String lSql = readSqlQuery("LAD");
    final NamedParameterJdbcTemplate lTemplate = createNamedParameterJdbcTemplate();

    return lTemplate.query(lSql, lParameterSource, new DogExtractor());
  }

I use the above method to load data for an integration test. 我使用上述方法加载数据以进行集成测试。 Unfortunality the size of the result list is about 300000 data rows. 不幸的是,结果列表的大小约为300000个数据行。

For my test it is ok to work only with 100 data rows. 对于我的测试,可以只处理100个数据行是可以的。 So I wrote a SQL Test file(Key LAD_TEST) that returns only 100 rows: 因此,我编写了一个SQL测试文件(键LAD_TEST),该文件仅返回100行:

SELECT 
*
FROM 
DOG
WHERE 
TO_CHAR(sell, 'dd.mm.yy') = TO_CHAR(:jdate,'dd.mm.yy')
and rownum <= 100

my question is, can I include anyhow that test sql(LAD_TEST) instead of the real production sql(LAD) without changing the production code here final String lSql = readSqlQuery("LAD"); 我的问题是,我可以在不更改生产代码的情况下包括测试sql(LAD_TEST)而不是实际生产sql(LAD)的final String lSql = readSqlQuery("LAD"); ??? ???

I am using jmockit in my testclass but that dao class(mDogDao) I am talking about is not mocked... 我在测试类中使用jmockit,但我正在谈论的dao类(mDogDao)没有被嘲笑...

The call from my test: 我测试的电话:

List<Dog> lAllDog = mDogDao.loadAllDog(lNow.getTime());

Is there any way to manage this with jmockit without mocking mDogDao? 有没有办法在不模拟mDogDao的情况下使用jmockit进行管理?

Some advice? 一些忠告? Thx Stefan 斯蒂芬·史蒂芬(Thx Stefan)

您可以模拟NamedParameterJdbcTemplate类,并记录期望值,以便query(...)方法返回所需的测试数据。

Why would you want to query your live database in a unit test? 为什么要在单元测试中查询实时数据库?

What I always do is work against a seperate unit test database schema or an in-memory database. 我一直在做的是针对单独的单元测试数据库架构或内存数据库。 That way I am certain that a bug in my queries doesn't influence the data used by other people. 这样,我可以确定查询中的错误不会影响其他人使用的数据。

Even if you need a certain amount of test-data, you can always insert an extract of your data before your test and clean it up afterwards. 即使您需要一定数量的测试数据,也可以始终在测试之前插入数据摘录,然后再进行清理。

Moreover this way your unit tests are also run in isolation. 而且,通过这种方式,您的单元测试也可以独立运行。 If one tests modifies data, your other tests don't suffer from the possible consequences. 如果一个测试修改了数据,则其他测试不会遭受可能的后果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM