Good evening, first time faced with mock objects, I tryed to test my application but nothing happened. I should test the work of ATM. I have got 2 interfaces which are not implemented (Account and Card) and one class. ATM (which has got empty methods). My task was too implement methods in class ATM, and to test all methods using mock objects. when i am trying to run this in netbeans i am getting just assembley succesfully, but i need green line which indicates whether my test passed or not.
something like this:
unitTestsResultsInNetBeans73WithHamcrestBeforeJUnitClasspath.jpg
Here is my code i guess everything i did properly.
1. Card inerface
2.Account interfacepackage myatm; public interface Card { public boolean isBlocked(); // checks whther card is blocked or not public Account getAccount(); // returns the balance connected with this card public boolean checkPin(int pinCode); // checks the property of password }
3.ATM class which i implementedpackage myatm; public interface Account { public double getBalance(); // returns current balance public double withdrow(double amount); // returns the sum which was taken. }
4.ATM test (Here i wrote some test)package myatm; public class ATM { public double money; ATM(double moneyInATM) { // we can set up the number of money in ATM money = moneyInATM; } // checks pin code and card status(blocked or not) // if blocked should send exception // if pin is not correct should send exception too public boolean validateCard(Card card, int pinCode) { boolean ret = false; if ((card.checkPin(pinCode) == false) && (card.isBlocked() == false)) { ret = false; } else { if ((card.checkPin(pinCode) == true) && (card.isBlocked() == true)) ret = true; } return ret; } Account acc = null; // returns the total ammount of money public double checkBalance() { return acc.getBalance(); } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public Account getAcc() { return acc; } public void setAcc(Account acc) { this.acc = acc; } // method which is taking money from the account. // Should check if sum is less then money in atm public double getCash(double amount) { double sum = amount; if (this.checkBalance() > acc.getBalance()) { sum = (acc.getBalance() - sum); } else if (this.checkBalance() < acc.getBalance()) { throw new IllegalArgumentException("Not enough money in ATM"); } else if (sum > acc.getBalance()) { throw new UnsupportedOperationException( "Not enought money on your account"); } return sum; } }
6.MyATM class where i am creating objects and calling methods.package myatm; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) public class ATMtest { // Class Under Test ATM atm; @Before public void setup() { Card card = mock(Card.class); Account acc = mock(Account.class); when(card.isBlocked()).thenReturn(Boolean.FALSE); when(card.checkPin(1234)).thenReturn(Boolean.TRUE); when(acc.getBalance()).thenReturn((double) 1); atm = new ATM(1500); atm.setAcc(acc); } @Test public void testCheckBalance() { /* Test */ double result = atm.checkBalance(); /* Asserts */ Assert.assertEquals((double) 1, result, .001); } }
[CODE]
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
[CODE]