Theaim of this lab is to review some basic Java programming techniques andto introduce the JUnit testing framework. This tutorial introduces thelatest version, JUnit 4, and its integration in the Eclipse IDE. Thejava files required for this exercise can be downloaded from the linkson this web page.
int balance
and String
accountName to class BankAccount to represent the value of theaccount balance, and the name of the customer. int
maxBalance and int
minBalance to represent some history of the bankaccount. These variables remember the maximum and minimumbalances ever held by the account since it was created. public void deposit(int amount) {}
public void withdraw(int amount) {}
public void chargeInterest(double rate) {}
public boolean isOverdrawn() { return true; }
Calculator c;
@Before
public void setUp () throws Exception {
c = new Calculator();
}
@After
public void tearDown () throws Exception {
}
@Test
public void testAdd () {
c.add(4);
assertEquals(4, c.getResult());
}
c.add(4);
c.multiply(3);
assertEquals(12,c.getResult());
This section contains further optional work forinterested students. Testing exceptions will also be revisited later inthe unit. The tests you have written so far can be thought of as sanitytests. Officially, they are called normal test cases. They test normaloperation of the methods, but do not cover any particularly difficultbehaviour. When testing a class, you must also consider boundary casesand exceptional cases, since these are the areas where errors are mostlikely to occur.
@Test(expected = ArithmeticException.class)
public void testDivideZero() { c.add(4); c.divide(0); /* this call should throw an exception*/ }
Copy your BankAccount classto a new class, say AdvancedBankAccount.java andwrite a new tester AdvancedBankAccountTest.java tohandle exceptionalcases as you think appropriate.
| ||
|
|