Java 學習記錄107 — Introduction to Unit Testing with JUnit

package bankAcoount;public class BankAccount {
private String firstName;
private String lastName;
private double balance;
public BankAccount(String firstName, String lastName, double balance) {
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}
// The branch argument is true if the customer is performing the transaction
// at a branch, with a teller, otherwise is at an ATM
public double deposit(double amount, boolean branch) {
balance += amount;
return balance;
}
// The branch argument is true if the customer is performing the transaction
// at a branch, with a teller, otherwise is at an ATM
public double withdraw(double amount, boolean branch) {
balance -= amount;
return balance;
}
public double getBalance() {
return balance;
}
// More methods that use firstName, lastName, and perform other functions
}
package bankAcoount;import org.junit.Test;import static org.junit.Assert.*;public class BankAccountTest {    @Test
public void deposit() {
}
@Test
public void withdraw() {
}
@Test
public void getBalance() {
}
}
package bankAcoount;import org.junit.Test;import static org.junit.Assert.*;public class BankAccountTest {    @Test
public void deposit() {
fail("Not yet implement");
}
@Test
public void withdraw() {
}
@Test
public void getBalance() {
}
@Test
public void dummyTest() {
assertEquals(20, 50);
}
}
@Test
public void deposit() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00);
double balance = account.deposit(200.00, true);
assertEquals(1200.00, balance, 0);
assertEquals(1200.00, account.getBalance(), 0);
}
package bankAcoount;import org.junit.Test;import static org.junit.Assert.*;public class BankAccountTest {    @Test
public void deposit() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00);
double balance = account.deposit(200.00, true);
assertEquals(1200.00, balance, 0);
}
@Test
public void withdraw() {
fail("Not yet implement");
}
@Test
public void getBalance() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00);
account.deposit(200.00, true);
assertEquals(1200.00, account.getBalance(), 0);
}
}
package bankAcoount;import org.junit.Test;import static org.junit.Assert.*;public class BankAccountTest {    @Test
public void deposit() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00);
double balance = account.deposit(200.00, true);
assertEquals(1200.00, balance, 0);
}
@Test
public void withdraw() {
fail("Not yet implement");
}
@Test
public void getBalance_deposit() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00);
account.deposit(200.00, true);
assertEquals(1200.00, account.getBalance(), 0);
}
@Test
public void getBalance_withdraw() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00);
account.withdraw(200.00, true);
assertEquals(800.00, account.getBalance(), 0);
}
}
package bankAcoount;public class BankAccount {
private String firstName;
private String lastName;
private double balance;
public static final int CHECKING = 1;
public static final int SAVING = 1;
private int typeOfAccount; public BankAccount(String firstName, String lastName, double balance, int typeOfAccount) {
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
this.typeOfAccount = typeOfAccount;
}
// The branch argument is true if the customer is performing the transaction
// at a branch, with a teller, otherwise is at an ATM
public double deposit(double amount, boolean branch) {
balance += amount;
return balance;
}
// The branch argument is true if the customer is performing the transaction
// at a branch, with a teller, otherwise is at an ATM
public double withdraw(double amount, boolean branch) {
balance -= amount;
return balance;
}
public double getBalance() {
return balance;
}
public boolean isChecking() {
return typeOfAccount == CHECKING;
}
// More methods that use firstName, lastName, and perform other functions
}
package bankAcoount;import org.junit.Test;import static org.junit.Assert.*;public class BankAccountTest {    @Test
public void deposit() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00, BankAccount.CHECKING);
double balance = account.deposit(200.00, true);
assertEquals(1200.00, balance, 0);
}
@Test
public void withdraw() {
fail("Not yet implement");
}
@Test
public void getBalance_deposit() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00, BankAccount.CHECKING);
account.deposit(200.00, true);
assertEquals(1200.00, account.getBalance(), 0);
}
@Test
public void getBalance_withdraw() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00, BankAccount.CHECKING);
account.withdraw(200.00, true);
assertEquals(800.00, account.getBalance(), 0);
}
@Test
public void isChecking_true() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00, BankAccount.CHECKING);
assertTrue(account.isChecking());
}
}
@Test
public void isChecking_true() {
BankAccount account = new BankAccount("Tim", "Jimmy", 1000.00, BankAccount.SAVING);
assertTrue("The account is NOT a checking account", account.isChecking());
}
package bankAcoount;import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;public class BankAccountTest { private BankAccount account; @Before
public void setup() {
account = new BankAccount("Tim", "Jimmy", 1000.00, BankAccount.CHECKING);
System.out.println("Running a test...");
}
@Test
public void deposit() {
double balance = account.deposit(200.00, true);
assertEquals(1200.00, balance, 0);
}
@Test
public void withdraw() {
fail("Not yet implement");
}
@Test
public void getBalance_deposit() {
account.deposit(200.00, true);
assertEquals(1200.00, account.getBalance(), 0);
}
@Test
public void getBalance_withdraw() {
account.withdraw(200.00, true);
assertEquals(800.00, account.getBalance(), 0);
}
@Test
public void isChecking_true() {
assertTrue("The account is NOT a checking account", account.isChecking());
}
}
package bankAcoount;import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;public class BankAccountTest { private BankAccount account; @BeforeClass
public static void beforeClass(){
System.out.println("This executes before any test cases");
}
@Before
public void setup() {
account = new BankAccount("Tim", "Jimmy", 1000.00, BankAccount.CHECKING);
System.out.println("Running a test...");
}
@Test
public void deposit() {
double balance = account.deposit(200.00, true);
assertEquals(1200.00, balance, 0);
}
@Test
public void withdraw() {
fail("Not yet implement");
}
@Test
public void getBalance_deposit() {
account.deposit(200.00, true);
assertEquals(1200.00, account.getBalance(), 0);
}
@Test
public void getBalance_withdraw() {
account.withdraw(200.00, true);
assertEquals(800.00, account.getBalance(), 0);
}
@Test
public void isChecking_true() {
assertTrue("The account is NOT a checking account", account.isChecking());
}
@AfterClass
public static void afterClass(){
System.out.println("This executes after any test cases.");
}
}
package bankAcoount;import org.junit.*;import static org.junit.Assert.*;public class BankAccountTest {    private BankAccount account;
private static int count;
@BeforeClass
public static void beforeClass(){
System.out.println("This executes before any test cases. Count = " + count++);
}
@Before
public void setup() {
account = new BankAccount("Tim", "Jimmy", 1000.00, BankAccount.CHECKING);
System.out.println("Running a test...");
}
@Test
public void deposit() {
double balance = account.deposit(200.00, true);
assertEquals(1200.00, balance, 0);
}
@Test
public void withdraw() {
fail("Not yet implement");
}
@Test
public void getBalance_deposit() {
account.deposit(200.00, true);
assertEquals(1200.00, account.getBalance(), 0);
}
@Test
public void getBalance_withdraw() {
account.withdraw(200.00, true);
assertEquals(800.00, account.getBalance(), 0);
}
@Test
public void isChecking_true() {
assertTrue("The account is NOT a checking account", account.isChecking());
}
@After
public void teardown(){
System.out.println("Count = " + count++);
}
@AfterClass
public static void afterClass(){
System.out.println("This executes after any test cases. Count = " + count++);
}
}
// The branch argument is true if the customer is performing the transaction
// at a branch, with a teller, otherwise is at an ATM
public double withdraw(double amount, boolean branch) {
if ((amount > 500.00) && !branch) {
throw new IllegalArgumentException();
}
balance -= amount;
return balance;
}
@Test
public void withdraw() {
double balance = account.withdraw(600.00, true);
assertEquals(400.00, balance, 0);
}
@Test
public void withdraw_branch() {
double balance = account.withdraw(600.00, true);
assertEquals(400.00, balance, 0);
}
@Test
public void withdraw_notBranch() {
double balance = account.withdraw(600.00, false);
assertEquals(400.00, balance, 0);
}
@Test(expected = IllegalArgumentException.class)
public void withdraw_notBranch() {
double balance = account.withdraw(600.00, false);
assertEquals(400.00, balance, 0);
}
@Test
public void withdraw_branch() {
account.withdraw(600.00, true);
}
@Test(expected = IllegalArgumentException.class)
public void withdraw_notBranch() {
account.withdraw(600.00, false);
}
@Test //(expected = IllegalArgumentException.class)
public void withdraw_notBranch() {
try {
account.withdraw(600.00, false);
} catch (IllegalArgumentException ignored) {

}
}
@Test //(expected = IllegalArgumentException.class)
public void withdraw_notBranch() {
try {
account.withdraw(600.00, true);
fail("Should have thrown and IllegalArgumentException");
} catch (IllegalArgumentException ignored) {
}
}

上面代碼全都紀錄在我的 Github

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store