简体   繁体   中英

mock abstract method of extended class with jmockit

I have a method inside my Dao class like this:

  @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.

For my test it is ok to work only with 100 data rows. So I wrote a SQL Test file(Key LAD_TEST) that returns only 100 rows:

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"); ???

I am using jmockit in my testclass but that dao class(mDogDao) I am talking about is not mocked...

The call from my test:

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

Is there any way to manage this with jmockit without mocking mDogDao?

Some advice? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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