Reputation: 17
public class AccountModel extends AbstractModel {
public static BigDecimal balance = new BigDecimal(0.00);
public static BigDecimal balanceEuro = new BigDecimal(0.00);
public static BigDecimal balanceYen = new BigDecimal(0.00);
// deposit method
public static BigDecimal deposit(double amount) {
balance = balance.add(new BigDecimal(amount));
return balance;
}
// withdraw method
public static void withdraw(BigDecimal withdraw) {
balance = balance.subtract(withdraw);
}
public static BigDecimal euro() {
balanceEuro = balance.multiply(new BigDecimal(0.79));
return balanceEuro;
}
public static BigDecimal yen() {
balanceYen = balance.multiply(new BigDecimal(94.1));
return balanceYen;
}
public static BigDecimal balance() {
return balance;
}
@Override
public void notifyChange(ModelEvent me) {
// TODO Auto-generated method stub
}
}
public class AccountModelTest {
AccountModel accTest = new AccountModel();
// test deposit method
@Test
public void testDeposit() {
// BigDecimal balance = new BigDecimal(0.00);
System.out.println("Deposit");
accTest.deposit(30.00);
accTest.deposit(100.00);
// excepted balance
BigDecimal expectedBalance = new BigDecimal(130.00);
System.out.println("Expected: " + expectedBalance.setScale(2, BigDecimal.ROUND_HALF_DOWN) + " Actual: "
+ accTest.balance().setScale(2, BigDecimal.ROUND_HALF_DOWN));
BigDecimal balance = new BigDecimal(0.00);
assertEquals(expectedBalance.setScale(2, BigDecimal.ROUND_HALF_DOWN),
accTest.balance().setScale(2, BigDecimal.ROUND_HALF_DOWN));
System.out.println("");
}
// test withdraw method
@Test
public void testWithdraw() {
System.out.println("Withdraw");
AccountModel accTest1 = new AccountModel();
accTest1.deposit(40.00);
accTest1.withdraw(new BigDecimal(30.00));
BigDecimal expectedBalance1 = new BigDecimal(10.00);
System.out.println("Expected: " + expectedBalance1.setScale(2, BigDecimal.ROUND_HALF_DOWN) + " Actual: "
+ accTest1.balance().setScale(2, BigDecimal.ROUND_HALF_DOWN));
assertEquals(expectedBalance1.setScale(2, BigDecimal.ROUND_HALF_DOWN),
accTest1.balance().setScale(2, BigDecimal.ROUND_HALF_DOWN));
}
}
So I'm doing 2 separate test cases for the withdraw method and deposit method. When I implement 1 test case The deposit method works fine, and also the withdraw method works fine. But when I run the test like shown above, the test always fails. The output is
Withdraw Expected: 10.00 Actual: 10.00
Deposit Expected: 130.00 Actual: 140.00 (should be 130.00)
Upvotes: 2
Views: 91
Reputation: 12178
every test show run with clean context, you are sharing AccountModel accTest
with multiple tests. you should set up and clean the context each time a test run.
public class AccountModelTest {
AccountModel accTest;
@Before // this code will run before any test start, it will create new model for each test
void setUp() {
AccountModel accTest = new AccountModel();
}
Upvotes: 1
Reputation: 44980
Since you are using static fields inside AccountModel
class their value is shared between all AccountModel
instances. Make them private non-static:
private BigDecimal balance = BigDecimal.ZERO;
private BigDecimal balanceEuro = BigDecimal.ZERO;
private BigDecimal balanceYen = BigDecimal.ZERO;
JUnit is creating new instance of AccountModelTest
class for every @Test
with new AccountModel
so starting balance will be zero for every test.
Upvotes: 2