From 1fff7e827f70b941a6d2a3b00d0ee5d9fdeff264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Sk=C3=B8ien?= Date: Wed, 25 Feb 2026 11:50:28 +0100 Subject: [PATCH] add unit tests for Player and improved tests for Exchange and Portfolio --- .../idi/idatt2003/gruppe42/ExchangeTest.java | 3 +- .../idi/idatt2003/gruppe42/PlayerTest.java | 29 +++++++++++++++++ .../idi/idatt2003/gruppe42/PortfolioTest.java | 32 ++++++++++--------- 3 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PlayerTest.java diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/ExchangeTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/ExchangeTest.java index fde419a..c79e9c1 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/ExchangeTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/ExchangeTest.java @@ -11,11 +11,10 @@ import org.junit.jupiter.api.Test; public class ExchangeTest { - private Exchange exchange; @BeforeEach - public void setUp() { + void setUp() { Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100")); exchange = new Exchange("NYSE", List.of(stock)); } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PlayerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PlayerTest.java new file mode 100644 index 0000000..9ea3a0b --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PlayerTest.java @@ -0,0 +1,29 @@ +package edu.ntnu.idi.idatt2003.gruppe42; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; +import java.math.BigDecimal; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class PlayerTest { + private Player player; + + @BeforeEach + void setUp() { + player = new Player("John Doe", new BigDecimal("10000")); + } + + @Test + void addMoneyTest() { + player.addMoney(new BigDecimal("5000")); + assertEquals(new BigDecimal("15000"), player.getMoney()); + } + + @Test + void withdrawMoneyTest() { + player.withdrawMoney(new BigDecimal("5000")); + assertEquals(new BigDecimal("5000"), player.getMoney()); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java index 5bb56d8..ca1b060 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java @@ -1,33 +1,35 @@ package edu.ntnu.idi.idatt2003.gruppe42; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + import edu.ntnu.idi.idatt2003.gruppe42.Model.Portfolio; import edu.ntnu.idi.idatt2003.gruppe42.Model.Share; import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; -import org.junit.jupiter.api.Test; - import java.math.BigDecimal; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class PortfolioTest { + private Portfolio portfolio; + private Stock stock; + private Share share; + + @BeforeEach + void setUp() { + portfolio = new Portfolio(); + stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("10000")); + share = new Share(stock, new BigDecimal("10"), new BigDecimal("15000")); + } @Test - public void addShareTest() { - Portfolio portfolio = new Portfolio(); - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("10000")); - Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("15000")); - + void addShareTest() { boolean result = portfolio.addShare(share); assertTrue(portfolio.contains(share)); } @Test - public void removeShareTest() { - Portfolio portfolio = new Portfolio(); - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("10000")); - Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("15000")); - + void removeShareTest() { portfolio.addShare(share); boolean result = portfolio.removeShare(share); assertFalse(portfolio.contains(share));