From b42502d37941ef3704f4951c3c2f1e43ebe0e892 Mon Sep 17 00:00:00 2001 From: Elisabeth Berg Date: Tue, 10 Mar 2026 20:06:17 +0100 Subject: [PATCH] Added more tests to PortfolioTest class --- src/test/java/ExchangeTest.java | 9 +++--- src/test/java/PortfolioTest.java | 50 ++++++++++++++++++++++++++++++-- src/test/java/StockTest.java | 5 ++-- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/test/java/ExchangeTest.java b/src/test/java/ExchangeTest.java index d3bcd62..eb3ad47 100644 --- a/src/test/java/ExchangeTest.java +++ b/src/test/java/ExchangeTest.java @@ -1,19 +1,18 @@ -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; -import org.junit.Before; -import org.junit.Test; - public class ExchangeTest { private Exchange exchange; private Stock apple; private Stock google; - @Before + @BeforeAll public void setUp() { apple = new Stock("AAPL", "Apple", new BigDecimal("100")); google = new Stock("GOOGL", "Google", new BigDecimal("200")); diff --git a/src/test/java/PortfolioTest.java b/src/test/java/PortfolioTest.java index 57ecb47..36f7419 100644 --- a/src/test/java/PortfolioTest.java +++ b/src/test/java/PortfolioTest.java @@ -6,7 +6,7 @@ public class PortfolioTest { @Test - void testAddShare() { + void testAddSharePortfolio() { Portfolio portfolio = new Portfolio(); Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); @@ -19,7 +19,7 @@ void testAddShare() { } @Test - void testRemoveShare() { + void testRemoveSharePortfolio() { Portfolio portfolio = new Portfolio(); Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); @@ -33,5 +33,51 @@ void testRemoveShare() { assertFalse(portfolio.contains(share)); } + @Test + void testGetSharesPortfolio() { + Portfolio portfolio = new Portfolio(); + + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); + + portfolio.addShare(share); + + var shares = portfolio.getShares(); + + assertEquals(1, shares.size()); + assertTrue(shares.contains(share)); + } + + @Test + void testGetSharesBySymbolPortfolio() { + Portfolio portfolio = new Portfolio(); + + Stock stock1 = new Stock("AAPL", "Apple", new BigDecimal("150")); + Stock stock2 = new Stock("GOOGL", "Google", new BigDecimal("200")); + + Share share1 = new Share(stock1, new BigDecimal("10"), new BigDecimal("140")); + Share share2 = new Share(stock2, new BigDecimal("10"), new BigDecimal("190")); + + portfolio.addShare(share1); + portfolio.addShare(share2); + + var result = portfolio.getShares("AAPL"); + + assertEquals(1, result.size()); + assertTrue(result.contains(share1)); + } + + @Test + void testContainsPortfolio() { + Portfolio portfolio = new Portfolio(); + + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); + + portfolio.addShare(share); + + assertTrue(portfolio.contains(share)); + } + } diff --git a/src/test/java/StockTest.java b/src/test/java/StockTest.java index 5f587f9..1bfcc2a 100644 --- a/src/test/java/StockTest.java +++ b/src/test/java/StockTest.java @@ -1,9 +1,8 @@ -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; import java.math.BigDecimal; -import org.junit.Test; - public class StockTest {