-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,75 +1,96 @@ | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import Model.Share; | ||
| import Model.Stock; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
|
|
||
| public class ShareTest { | ||
|
|
||
| @Test | ||
| void testShareConstructor() { | ||
| private Stock stock; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| } | ||
|
|
||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| BigDecimal quantity = new BigDecimal("10"); | ||
| BigDecimal purchasePrice = new BigDecimal("140"); | ||
| // ---- Positive tests ---- | ||
|
|
||
| Share share = new Share(stock, quantity, purchasePrice); | ||
| @Test | ||
| void testShareConstructorNotNull() { | ||
| Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); | ||
| 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); | ||
| assertEquals(stock, share.getStock()); | ||
| } | ||
|
|
||
| @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); | ||
| assertEquals(new BigDecimal("10"), share.getQuantity()); | ||
| } | ||
|
|
||
| @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); | ||
| assertEquals(new BigDecimal("140"), share.getPurchasePrice()); | ||
| } | ||
|
|
||
| @Test | ||
| void testNullStock() { | ||
| void testPurchasePriceZeroAllowed() { | ||
| // buying at price 0 is an edge case but not invalid | ||
| Share share = new Share(stock, new BigDecimal("5"), BigDecimal.ZERO); | ||
| assertEquals(BigDecimal.ZERO, share.getPurchasePrice()); | ||
| } | ||
|
|
||
| Share share = new Share(null, new BigDecimal("10"), new BigDecimal("100")); | ||
| // ---- Negative tests ---- | ||
|
|
||
| assertNull(share.getStock()); | ||
| @Test | ||
| void testNullStockThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| new Share(null, new BigDecimal("10"), new BigDecimal("100")) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void testNegativePrice() { | ||
| void testNullQuantityThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| new Share(stock, null, new BigDecimal("100")) | ||
| ); | ||
| } | ||
|
|
||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("-50")); | ||
| @Test | ||
| void testZeroQuantityThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| new Share(stock, BigDecimal.ZERO, new BigDecimal("100")) | ||
| ); | ||
| } | ||
|
|
||
| assertEquals(new BigDecimal("-50"), share.getPurchasePrice()); | ||
| @Test | ||
| void testNegativeQuantityThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| new Share(stock, new BigDecimal("-5"), new BigDecimal("100")) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void testNullPurchasePriceThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| new Share(stock, new BigDecimal("10"), null) | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void testNegativePurchasePriceThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| new Share(stock, new BigDecimal("10"), new BigDecimal("-50")) | ||
| ); | ||
| } | ||
| } |
Binary file not shown.