Skip to content

Commit

Permalink
Added more tests to ShareTest
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed May 24, 2026
1 parent b5da0cf commit c599f28
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
87 changes: 54 additions & 33 deletions src/test/java/ShareTest.java
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 modified target/test-classes/ShareTest.class
Binary file not shown.

0 comments on commit c599f28

Please sign in to comment.