From 98bdd9f63151085c600f7cdafd3eb6980aeed77c Mon Sep 17 00:00:00 2001 From: = Date: Mon, 25 May 2026 19:50:42 +0200 Subject: [PATCH] Update StockTest.java --- .../idatt2003/g40/mappe/model/StockTest.java | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/StockTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/StockTest.java index c5f907d..8c7f48d 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/StockTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/StockTest.java @@ -1,15 +1,20 @@ package edu.ntnu.idi.idatt2003.g40.mappe.model; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.math.BigDecimal; import java.util.List; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - final class StockTest { + /** + * Stock to use for testing. + * */ private Stock testStock; @BeforeEach @@ -23,6 +28,28 @@ void constructorSetsSymbolAndCompany() { assertEquals("Apple Inc.", testStock.getCompany()); } + @Test + void constructorThrowsExceptionOnIllegalArguments() { + assertDoesNotThrow( + () -> new Stock("AAPL", "APPLE INC.", new BigDecimal("100")) + ); + + assertThrows(IllegalArgumentException.class, + () -> new Stock("", "APPLE INC.", new BigDecimal("100")) + ); + + assertThrows(IllegalArgumentException.class, + () -> new Stock("AAPL", "", new BigDecimal("100")) + ); + + assertThrows(IllegalArgumentException.class, + () -> new Stock("AAPL", "APPLE INC.", new BigDecimal("0")) + ); + assertThrows(IllegalArgumentException.class, + () -> new Stock("AAPL", "APPLE INC.", null) + ); + } + @Test void addNewSalesPriceThenGetSalesPriceReturnsLastAddedPrice() { testStock.addNewSalesPrice(new BigDecimal("123.45")); @@ -39,11 +66,18 @@ void addNewSalesPriceTwiceGetSalesPriceReturnsMostRecent() { } @Test - void addNewSalesPriceDoesNotAllowNullCurrentImplementation() { - + void addNewSalesPriceThrowsExceptionOnIllegalArguments() { assertThrows(IllegalArgumentException.class, () -> { testStock.addNewSalesPrice(null); }); + + assertThrows(IllegalArgumentException.class, () -> { + testStock.addNewSalesPrice(new BigDecimal("0")); + }); + + assertThrows(IllegalArgumentException.class, () -> { + testStock.addNewSalesPrice(new BigDecimal("-10")); + }); } @Test