diff --git a/target/test-classes/ExchangeTest.java b/src/test/java/ExchangeTest.java similarity index 88% rename from target/test-classes/ExchangeTest.java rename to src/test/java/ExchangeTest.java index d3bcd62..eb3ad47 100644 --- a/target/test-classes/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 new file mode 100644 index 0000000..36f7419 --- /dev/null +++ b/src/test/java/PortfolioTest.java @@ -0,0 +1,83 @@ +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import java.math.BigDecimal; + +public class PortfolioTest { + + @Test + void testAddSharePortfolio() { + Portfolio portfolio = new Portfolio(); + + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); + + boolean result = portfolio.addShare(share); + + assertTrue(result); + assertTrue(portfolio.contains(share)); + } + + @Test + void testRemoveSharePortfolio() { + 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); + + boolean result = portfolio.removeShare(share); + + assertTrue(result); + 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/ShareTest.java b/src/test/java/ShareTest.java new file mode 100644 index 0000000..ba6dc76 --- /dev/null +++ b/src/test/java/ShareTest.java @@ -0,0 +1,71 @@ +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import java.math.BigDecimal; + + +public class ShareTest { + + @Test + void testShareConstructor() { + + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + BigDecimal quantity = new BigDecimal("10"); + BigDecimal purchasePrice = new BigDecimal("140"); + + Share share = new Share(stock, quantity, purchasePrice); + assertNotNull(share); + } + + @Test + void testGetStock() { + + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); + + Stock result = share.getStock(); + + assertEquals(stock, result); + } + + @Test + void testGetQuantity() { + + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); + + BigDecimal result = share.getQuantity(); + + assertEquals(new BigDecimal("10"), result); + } + + @Test + void testGetPurchasePrice() { + + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); + + BigDecimal result = share.getPurchasePrice(); + + assertEquals(new BigDecimal("140"), result); + } + + @Test + void testNullStock() { + + Share share = new Share(null, new BigDecimal("10"), new BigDecimal("100")); + + assertNull(share.getStock()); + } + + @Test + void testNegativePrice() { + + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("-50")); + + assertEquals(new BigDecimal("-50"), share.getPurchasePrice()); + } + + +} diff --git a/target/test-classes/StockTest.java b/src/test/java/StockTest.java similarity index 92% rename from target/test-classes/StockTest.java rename to src/test/java/StockTest.java index 5f587f9..1bfcc2a 100644 --- a/target/test-classes/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 { diff --git a/target/classes/PurchaseCalculator.class b/target/classes/PurchaseCalculator.class index c50d57d..bab65f2 100644 Binary files a/target/classes/PurchaseCalculator.class and b/target/classes/PurchaseCalculator.class differ