模拟对象和接口

时间:2010-05-11 08:35:51

标签: java unit-testing easymock

我是使用Mock Object进行单元测试的新手。我使用EasyMock。我试着理解这个例子:

import java.io.IOException;

public interface ExchangeRate {

    double getRate(String inputCurrency, String outputCurrency) throws IOException;

}

import java.io.IOException;


public class Currency {

    private String units;
    private long amount;
    private int cents;


    public Currency(double amount, String code) {
        this.units = code;
        setAmount(amount);
    }

    private void setAmount(double amount) {
        this.amount = new Double(amount).longValue();
        this.cents = (int) ((amount * 100.0) % 100);
    }

    public Currency toEuros(ExchangeRate converter) {
        if ("EUR".equals(units)) return this;
        else {
            double input = amount + cents/100.0;
            double rate;
            try {
                rate = converter.getRate(units, "EUR");
                double output = input * rate;
                return new Currency(output, "EUR");
            } catch (IOException ex) {
                return null;
            }
        }
    }

    public boolean equals(Object o) {
        if (o instanceof Currency) {
            Currency other = (Currency) o;
            return this.units.equals(other.units)
                    && this.amount == other.amount
                    && this.cents == other.cents;
        }
        return false;
    }

    public String toString() {
        return amount + "." + Math.abs(cents) + " " + units;
    }

}

import junit.framework.TestCase;
import org.easymock.EasyMock;
import java.io.IOException;

public class CurrencyTest extends TestCase {

    public void testToEuros() throws IOException {
        Currency testObject = new Currency(2.50, "USD");
        Currency expected = new Currency(3.75, "EUR");
        ExchangeRate mock = EasyMock.createMock(ExchangeRate.class);
        EasyMock.expect(mock.getRate("USD", "EUR")).andReturn(1.5);
        EasyMock.replay(mock);
        Currency actual = testObject.toEuros(mock);
        assertEquals(expected, actual);
    }

}

所以,我想知道如何在toEuros(..)方法中使用CurrencyRate。

rate = converter.getRate(units, "EUR");

未指定getRate(..)方法的行为,因为ExchangeRate是一个接口。

/********************************************************************************/

所以我尝试做自己的例子。以下是我的代码:

/**
 *Interface to access data
 */
public interface Dao {
    public boolean getEntityById(int id) throws SQLException;
}

/**
 *Business class do something in business layer
 */
public class Bussiness {
    private Dao dao;

    public Dao getDao() {
        return dao;
    }

    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public boolean doSomeThing(int id) throws SQLException {
        if(dao.getEntityById(id)) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) throws SQLException {
        Bussiness b = new Bussiness();
        b.doSomeThing(3);
    }
}


package tunl;

import java.sql.SQLException;

import org.easymock.EasyMock;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

    /**
     * This is my unit Test
     */
    @Test
    public class MyUnitTest {
        private Bussiness bussiness;
        private Dao mock;

        @BeforeTest
        public void setUp() {
            bussiness = new Bussiness();
            mock = EasyMock.createMock(Dao.class);// interface not class
            bussiness.setDao(mock);
        }

        public void testDoSomeThing() throws SQLException {
            EasyMock.expect(mock.getEntityById(3)).andReturn(true);
            EasyMock.replay(mock);
            Assert.assertTrue(bussiness.doSomeThing(3));
        }
    }

所以,单位Tess正确运行

但是当我想在Business Object中运行main方法时:

public static void main(String[] args) throws SQLException {
            Bussiness b = new Bussiness();
            b.doSomeThing(3);
}

我必须为Business添加构造函数。

public Bussiness() {
     dao = new DaoImpl();
}

所以,我的商务舱是:

package tunl;

import java.sql.SQLException;

public class Bussiness {
    private Dao dao;

    public Bussiness() {
        dao = new DaoImpl();
    }
    public Dao getDao() {
        return dao;
    }

    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public boolean doSomeThing(int id) throws SQLException {
        if(dao.getEntityById(id)) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) throws SQLException {
        Bussiness b = new Bussiness();
        b.doSomeThing(3);
    }
}

我还要实现Dao界面:

package tunl;

import java.sql.SQLException;

public class DaoImpl implements Dao {

    @Override
    public boolean getEntityById(int id) throws SQLException {
        if(id == 3) {
            System.out.println("System input 3 ");
            return true;
        }
        System.out.println("You have to input  3 ");
        return false;
    }

}

在设计中,您总是为将要测试的所有类创建接口(如DaoImpl)! 这是正确的吗?

1 个答案:

答案 0 :(得分:3)

EasyMock根据界面创建模拟对象。模拟对象实现接口的所有方法以及您指定的那些方法(例如,使用expect),它在调用时“重放”指定的行为。

创建模拟对象时,它处于录制模式。这条线

EasyMock.expect(mock.getRate("USD", "EUR")).andReturn(1.5);

指定当使用给定参数调用mock.getRate时,它将返回1.5。然后通过调用

将对象放入重放模式
EasyMock.replay(mock);

所有这些都在documentation中详细解释。

更新到您的评论 - Currency在此处传递ExchangeRate的实例:

public Currency toEuros(ExchangeRate converter) { ... }

它关心的是它获得了一个实现该接口的对象,所以

rate = converter.getRate(units, "EUR");

可以被调用。然后,测试方法将其创建的模拟对象传递给货币对象:

Currency actual = testObject.toEuros(mock);

希望这会有所帮助;如果没有,也许你可以阅读一些关于OOP,接口和继承的介绍性文本,以便更好地理解。

在答案的代码示例中,Dao对象应该传递给Bussiness而不是内部创建,因为后者有效地阻止了单元测试。

public static void main(String[] args) throws SQLException {
        Bussiness b = new Bussiness();
        b.setDao(new DaoImpl());
        b.doSomeThing(3);
}

您还可以将参数化构造函数添加到Bussiness,以便在一步而不是两步中进行初始化。