Skip to content

Commit

Permalink
test: Adding PurchaseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed Mar 6, 2026
1 parent eea0cf2 commit ed67b31
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/test/java/millions/PurchaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,39 @@

import static org.junit.jupiter.api.Assertions.*;

class PurchaseTest {}
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;

class PurchaseTest {
@Test
public void testHappyPath() {
Stock stock = new Stock("TestStock", "TST", BigDecimal.valueOf(10));
Share share = new Share(stock, 2, BigDecimal.valueOf(10));
Player player = new Player("TestPlayer", BigDecimal.valueOf(100));
Purchase purchase = new Purchase(share, 1);
purchase.commit(player);
assertTrue(purchase.isCommitted());

// 1 less because of tax
assertEquals(79, player.getMoney().intValue());
assertTrue(player.getPortfolio().getShares().contains(share));
}

@Test
public void testNullsAndInvalid() {

Stock stock = new Stock("TestStock", "TST", BigDecimal.valueOf(10));
Share share = new Share(stock, 2, BigDecimal.valueOf(10));
Player player = new Player("TestPlayer", BigDecimal.valueOf(100));
Purchase purchase = new Purchase(share, 1);

// Double commit
purchase.commit(player);
assertThrows(IllegalStateException.class, () -> purchase.commit(player));

// Insufficient funds
Player poorPlayer = new Player("PoorPlayer", BigDecimal.valueOf(10));
Purchase expensivePurchase = new Purchase(new Share(stock, 20, BigDecimal.valueOf(10)), 1);
assertThrows(IllegalStateException.class, () -> expensivePurchase.commit(poorPlayer));
}
}

0 comments on commit ed67b31

Please sign in to comment.