Mockito:如何通过嘲笑来测试我的Dao? [英] Mockito : How to test my Dao with mocking?

查看:205
本文介绍了Mockito:如何通过嘲笑来测试我的Dao?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是junit和TDD的新手.我正计划使用Mockito来测试dao.

I am newbie to junit and TDD. I am planning to use Mockito to test my dao.

Dao界面:

package com.test.SpringApp.dao;

import java.util.List;

import com.test.SpringApp.bean.Account;
import com.test.SpringApp.bean.Person;

public interface TestDao {
    List<Account> getAccountDetails(int account_id);
    Person getPersonDetails(int person_id);
}

DaoImpl类代码:

package com.test.SpringApp.dao;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.test.SpringApp.bean.Account;
import com.test.SpringApp.bean.Person;

public class TestDaoImpl implements TestDao {
    private static final Logger logger = Logger.getLogger(TestDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public List<Account> getAccountDetails(int account_id) {
        try {
            String query = "select * from account where account_id=?";
            SqlRowSet rowset = jdbcTemplate.queryForRowSet(query,account_id);
            Account account = null;
            List<Account> accountDetails = new ArrayList<Account>();
            while (rowset.next()) {
                account = new Account();
                account.setAccountId(rowset.getInt("accountid"));
                account.setAccountType(rowset.getString("accounttype"));
                accountDetails.add(account);
            }
            return accountDetails;
        } catch (Exception e) {
            // TODO: handle exception
            logger.info("Error :" + e.getMessage());
            return null;
        }
    }

    private Person getPersonDetails(int person_id) {
        try {
            String query = "select * from Person where person_id=?";
            SqlRowSet rowset = jdbcTemplate.queryForRowSet(query,person_id);
            Person person = null;
            while (rowset.next()) {
                person = new Person();
                person.setName(rowset.getString("name"));
                stage.setNumber(rowset.getInt("number"));
            }
            return person;
        } catch (Exception e) {
            // TODO: handle exception
            logger.info("Error :" + e.getMessage());
            return null;
        }
    }
}

我正在使用上述接口和类从数据库中获取帐户和个人详细信息.有人可以解释一下如何使用junit和Mockito为上述dao接口和类编写测试用例.

I am using above mentioned interface and class to get account and person details from database. Could someone please explain me how to write test cases for above mentioned dao interface and class using junit and Mockito.

我们将不胜感激:)

推荐答案

它似乎误解了测试中模拟的概念.

It seams that you misunderstood the concept of mocking in tests.

假设您有一个类A和一个类B.类A具有方法mA(),该方法使用类B中的方法mB()来实现其功能.

Assume you have a class A and a class B. Class A has a method mA(), that uses the method mB() from Class B for its functionality.

如果现在要测试mA(),则mA()将调用mB().在大多数情况下,这不是问题,但是假定类B是DAO,而mB()是将查询数据库的某些函数.然后,您将需要一个数据库以测试mA().现在假设您已经测试了mB(),而您只想测试mA().因此,您可以用模拟mB()的模拟代替B.

If you now want to test mA(), then mA() will invoke mB(). In most cases this is not a problem, but assume Class B is a DAO and mB() is some function that will query the Database. Then you will need a Database in order to test mA(). Now assume that you have already tests mB() and you only want to test mA(). So you can replace B by a mock , that "simulates" mB().

在您的情况下,BTestDao/TestDaoImpl.因此,如果您要模拟TestDao,则需要进行其他测试(A),因为您无法测试模拟的类

In your case, B is TestDao/TestDaoImpl. So if you want to mock TestDao, so you need someting else (A) to test, because you can not test the mocked class!

这篇关于Mockito:如何通过嘲笑来测试我的Dao?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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