Layout of the object and interface

I am new to Unit Test with Mock Object. I am using EasyMock. I am trying to understand this example:

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

}

So, I am wondering how to use the ExchangeRate currency in a method toEuros(..).

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

The behavior of the method is getRate(..)not specified, because it ExchangeRateis an interface.

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

Therefore, I try to make an example myself. Below are my codes:

/**
 *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));
        }
    }

So, the Tess block works correctly

But when I want to run the main method in a business object:

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

I need to add a constructor for Business.

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

So my business class:

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

I also need to implement the Dao interface:

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

}

In design, you always create an interface for all classes that will be tested (for example, DaoImpl) !!! So is that right?

+3
1

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

, ; , , , , .

Dao Bussiness, , .

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

Bussiness, .

+3

Source: https://habr.com/ru/post/1744895/


All Articles