Skip to content

Commit

Permalink
Added more tests to PurchaseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed May 24, 2026
1 parent 0187457 commit 8554f63
Showing 1 changed file with 77 additions and 16 deletions.
93 changes: 77 additions & 16 deletions src/test/java/PurchaseTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import java.math.BigDecimal;

Expand All @@ -11,32 +11,93 @@
import Model.Stock;

public class PurchaseTest {
Stock stock;
Share share;
Purchase purchase;

private Stock stock;
private Share share;
private Purchase purchase;
private Player player;

@BeforeEach
void setUp() {
this.stock = new Stock("symbol", "company", new BigDecimal(100));
this.share = new Share(this.stock, new BigDecimal(20), new BigDecimal(50));
this.purchase = new Purchase(this.share, 18);
stock = new Stock("AAPL", "Apple", new BigDecimal("100"));
share = new Share(stock, new BigDecimal("20"), new BigDecimal("50"));
purchase = new Purchase(share, 1);
player = new Player("Jane", new BigDecimal("500000"));
}

// ---- Positive tests ----

@Test
void commit_validPurchase_updatesPlayer() {
void testCommitDeductsMoneyFromPlayer() {
BigDecimal before = player.getMoney();
purchase.commit(player);
assertTrue(player.getMoney().compareTo(before) < 0);
}

// Arrange
Player player = new Player("Jane", new BigDecimal(500000));
BigDecimal startingMoney = player.getMoney();
@Test
void testCommitAddsShareToPortfolio() {
purchase.commit(player);
assertTrue(player.getPortfolio().contains(share));
}

@Test
void testCommitRecordedInArchive() {
purchase.commit(player);
assertTrue(player.getTransactionArchive().getAllTransactions().contains(purchase));
}

// Act
this.purchase.commit(player);
@Test
void testCommitSetsCommittedFlag() {
purchase.commit(player);
assertTrue(purchase.isCommitted());
}

// Assert
assertEquals(1, startingMoney.subtract(player.getMoney()).signum());
@Test
void testGetShareReturnsCorrectShare() {
assertEquals(share, purchase.getShare());
}

@Test
void testGetWeekReturnsCorrectWeek() {
assertEquals(1, purchase.getWeek());
}

// ---- Negative tests ----


@Test
void testCommitNullPlayerThrows() {
assertThrows(IllegalArgumentException.class, () ->
purchase.commit(null)
);
}

@Test
void testCommitInsufficientFundsThrows() {
Player poorPlayer = new Player("Broke", new BigDecimal("1"));
assertThrows(IllegalStateException.class, () ->
purchase.commit(poorPlayer)
);
}

@Test
void testCommitAlreadyCommittedThrows() {
purchase.commit(player);
assertThrows(IllegalStateException.class, () ->
purchase.commit(player)
);
}

@Test
void testConstructorWeekZeroThrows() {
assertThrows(IllegalArgumentException.class, () ->
new Purchase(share, 0)
);
}

@Test
void testConstructorNegativeWeekThrows() {
assertThrows(IllegalArgumentException.class, () ->
new Purchase(share, -5)
);
}
}

0 comments on commit 8554f63

Please sign in to comment.