Question

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

Dao Interface:

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 Class Code:

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;
        }
    }
}

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.

Help would be appreciated :)

Was it helpful?

Solution

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

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.

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().

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top