Skip to content

Commit

Permalink
iluwatar#1305: Added app test
Browse files Browse the repository at this point in the history
  • Loading branch information
hheanly123 committed Oct 15, 2023
1 parent a650644 commit 51af6a1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
21 changes: 14 additions & 7 deletions money/src/main/java/com/iluwatar/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public final class App {
private App() {
// Private constructor to hide the default public constructor
// Private constructor to hide the default public constructor
}

/**
Expand Down Expand Up @@ -44,19 +44,26 @@ public static void main(final String[] args) {
final Money allocationMoney = new Money(6_000, usd);

final Account[] accounts = new Account[2];

// Allocate the money equally among the accounts
final long allocatedAmount = allocationMoney.getAmount() / accounts.length;

accounts[0] = account;
accounts[1] = new Account(usd, eur);

allocationMoney.allocate(accounts, amount1, amount2);
// Deposit the allocated money into accounts
for (Account acc : accounts) {
acc.deposit(new Money(allocatedAmount, usd));
}

System.out.println("Allocated Balances:");
for (int i = 0; i < accounts.length; i++) {
System.out.println("Account "
+ (i + 1)
+ ": " + accounts[i].getPrimaryBalance().getAmount()
+ " "
+ accounts[i].getPrimaryBalance().getCurrency()
.getStringRepresentation());
+ (i + 1)
+ ": " + accounts[i].getPrimaryBalance().getAmount()
+ " "
+ accounts[i].getPrimaryBalance().getCurrency()
.getStringRepresentation());
}
}
}
38 changes: 38 additions & 0 deletions money/src/test/java/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import com.iluwatar.Account;
import com.iluwatar.App;
import com.iluwatar.Currency;
import com.iluwatar.Money;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;

public class AppTest {

@Test
public void testAppOutput() {
// Redirect the standard output to capture the printed messages
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

// Run the main method of the App class
App.main(new String[]{});

// Restore the standard output
System.setOut(System.out);

// Split the captured output into lines
String[] lines = outContent.toString().split(System.lineSeparator());

// Check the expected output
assertEquals("Primary Balance: 10000 USD", lines[0].trim());
assertEquals("Secondary Balance: 5000 EUR", lines[1].trim());
assertEquals("Allocated Balances:", lines[2].trim());
assertEquals("Account 1: 13000 USD", lines[3].trim());
assertEquals("Account 2: 3000 USD", lines[4].trim());
}
}

0 comments on commit 51af6a1

Please sign in to comment.