Skip to content

Instantly share code, notes, and snippets.

@Vaysman
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vaysman/84cf460159dae5dc94b2 to your computer and use it in GitHub Desktop.
Save Vaysman/84cf460159dae5dc94b2 to your computer and use it in GitHub Desktop.
package com.luxoft.bankapp.model;
import org.junit.Test;
import static org.junit.Assert.*;
public class AbstractAccountTest {
// dummy class for testing purpose
// there is no logic inside.
// I want test AbstractAccount not TestingAccount
private class TestingAccount extends AbstractAccount {
public TestingAccount(float balance) {
super(balance);
}
public TestingAccount() {
super();
}
}
// SUT - Subject Under Test
private TestingAccount sut;
// this is a test
@Test
public void testSetBalanceInConstructor() {
// create new object of TestingAccount
// this is very important to create new object for any test
sut = new TestingAccount(1.0f);
// check functionality
assertEquals(1.0f, sut.getBalance(), 0);
}
@Test
public void testSetBalanceToZeroByDefault() {
sut = new TestingAccount();
assertEquals(0.0f, sut.getBalance(), 0);
}
@Test
public void testDepositIncreaseBalance() {
sut = new TestingAccount(1.0f);
sut.deposit(2.0f);
// please read about assertEquals for floating point arguments
assertEquals(3.0f, sut.getBalance(), 0.00001f);
}
// this is a way to test exceoption
@Test(expected = IllegalArgumentException.class)
public void testNegativeDepositThrowsException() {
sut = new TestingAccount();
sut.deposit(-1.0f);
}
@Test(expected = IllegalArgumentException.class)
public void testZeroDepositThrowsException() {
sut = new TestingAccount();
sut.deposit(0.0f);
}
@Test(expected = IllegalArgumentException.class)
public void testNegativeThrowsException() {
sut = new TestingAccount(-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment