Java,春季:使用Mockito测试DAO的DataAccessException [英] Java, Spring: Testing DAOs for DataAccessException with Mockito

查看:208
本文介绍了Java,春季:使用Mockito测试DAO的DataAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试增加测试范围,所以我想知道,您将如何测试DAO中抛出的DataAccessExceptions,例如在简单的findAll方法中,该方法仅返回数据源中的所有数据?就我而言,我正在使用Spring JdbcTemplates.

I'm trying to increase my test coverage so I'm wondering, how would you go about testing for DataAccessExceptions being thrown in a DAO, for example in a simple findAll method which just returns all the data from your data source? In my case, I'm using Spring JdbcTemplates.

对于常规测试,我使用带有@Before注释的setUp方法,模拟使用的jdbcTemplate,在DAO中对其进行设置,并模拟所有jdbc调用.现在,对于诸如create方法之类的东西强制执行DataAccessException非常简单,只需在使用正确的主键调用create语句时抛出异常即可.

For general testing I have my setUp-method with an @Before annotation, mocking the jdbcTemplate used, setting it up in the DAO and mocking all jdbc calls. Now forcing a DataAccessException for something like a create method is pretty simple, just throw the exception when calling a create statement with the right primary keys.

但是,我真的不知道该如何处理不带任何输入参数的简单findAll方法之类的方法.测试有效的实现是很直接的,但是如果没有数据库连接而又不影响其他所有测试或方法,您将如何进行模拟呢?

However, I really have no idea how to handle this for methods like simple findAll methods which don't take any input parameters. Testing the valid implementation is straight forward, but how would you go about mocking having no DB connection without it affecting every other test or method?

这是我要测试的方法的具体实现:

This would be a concrete implementation of a method I'd like to test:

  public List<SomeObject> findAll() throws PersistenceException {
    final String sql = "SELECT * FROM SomeObject";

    try {
      return jdbcTemplate.query(sql, new JdbcSomeObjectMapper());
    } catch (DataAccessException ex) {
      LOG.error(ex.getMessage());
      throw new PersistenceException(ex.getMessage());
    }
  }

仅返回数据源中的所有对象.测试有效的调用很容易,因为我可以模拟jdbcTemplate.query调用,但是除非在检索数据时出现连接失败,否则我绝不会进入catch块,这就是我要测试的东西.

Which would just return all objects in the data source. Testing for a valid call is easy since I can just mock the jdbcTemplate.query call, but I'd never enter the catch block unless there's a connection failure while retrieving the data, and that's what I'd like to test.

推荐答案

使用Mockito,您可以模拟类以及该特定类的方法调用.当在其上调用特定方法时,也可以要求模拟对象抛出异常.首先,您必须模拟jdbcTemplate,然后对异常进行存根

Using Mockito you can mock a Class and the method calls of that particular Class. A mocked object can also be asked to throw an exception when particular methods are called on it. First you have to mock your jdbcTemplate, then stub your exception

   //mocking JdbcTemplate
JdbcTemplate template = Mockito.mock(JdbcTemplate.class);
Mockito.when(template.query(Mockito.anyString(), (RowMapper<YourClass>)  Mockito.any(RowMapper.class))).thenThrow(EmptyResultDataAccessException.class);

    //or using EasyMock
 EasyMock.expect(template.query(Mockito.anyString(), (RowMapper<YourClass>)  Mockito.any(RowMapper.class))).andThrow(new (typeofExecption));

这篇关于Java,春季:使用Mockito测试DAO的DataAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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