Skip to content

Commit

Permalink
Update ShareTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 25, 2026
1 parent e673691 commit e6a3914
Showing 1 changed file with 84 additions and 22 deletions.
106 changes: 84 additions & 22 deletions src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/ShareTest.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,114 @@
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.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.math.BigDecimal;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

final class ShareTest {

@Test
void constructorStoresAllValuesCorrectly() {
Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("150.00"));
/**
* Share to use for testing.
*/
private Share testShare;

/**
* Stock to use for testing.
* */
private Stock testStock;

@BeforeEach
void setUp() {
testStock = new Stock(
"AAPL",
"Apple Inc.",
new BigDecimal("150.00")
);
BigDecimal quantity = new BigDecimal("10");
BigDecimal purchasePrice = new BigDecimal("145.50");
testShare = new Share(testStock, quantity, purchasePrice);
}

@Test
void constructorStoresAllValuesCorrectly() {
assertSame(testStock, testShare.getStock());

assertEquals(0,
new BigDecimal("10")
.compareTo(testShare.getQuantity())
);

assertEquals(0,
new BigDecimal("145.50")
.compareTo(testShare.getPurchasePrice())
);
}

@Test
void constructorThrowsExceptionOnIllegalArguments() {
assertDoesNotThrow(
() -> new Share(testStock, new BigDecimal("1"),
new BigDecimal("100"))
);

assertThrows(IllegalArgumentException.class,
() -> new Share(null, new BigDecimal("1"), new BigDecimal("100"))
);

assertThrows(IllegalArgumentException.class,
() -> new Share(testStock, null, new BigDecimal("100"))
);

assertThrows(IllegalArgumentException.class,
() -> new Share(testStock, new BigDecimal("1"), null)
);

Share share = new Share(stock, quantity, purchasePrice);
assertThrows(IllegalArgumentException.class,
() -> new Share(testStock, new BigDecimal("0"),
new BigDecimal("100"))
);

assertSame(stock, share.getStock());
assertEquals(quantity, share.getQuantity());
assertEquals(purchasePrice, share.getPurchasePrice());
assertThrows(IllegalArgumentException.class,
() -> new Share(testStock, new BigDecimal("1"), new BigDecimal("0"))
);
}

@Test
void shareSupportsDecimalValues() {
Stock stock = new Stock("TSLA", "Tesla Inc.", new BigDecimal("200.00"));

Share share = new Share(
stock,
Share testShare2 = new Share(
testStock,
new BigDecimal("2.5"),
new BigDecimal("198.75")
);

assertEquals(new BigDecimal("2.5"), share.getQuantity());
assertEquals(new BigDecimal("198.75"), share.getPurchasePrice());
assertEquals(0,
new BigDecimal("2.5")
.compareTo(testShare2.getQuantity())
);

assertEquals(0,
new BigDecimal("198.75")
.compareTo(testShare2.getPurchasePrice())
);
}

@Test
void getStockReturnsCorrectStockObject() {
Stock stock = new Stock("NVDA",
"NVIDIA Corporation",
new BigDecimal("875.40"));
Share share = new Share(stock,
new BigDecimal("4"),
new BigDecimal("850.00"));

assertSame(stock, share.getStock());
assertEquals("NVDA", share.getStock().getSymbol());
assertEquals("NVIDIA Corporation", share.getStock().getCompany());
assertSame(testStock, testShare.getStock());
assertEquals(
"AAPL",
testShare.getStock().getSymbol()
);

assertEquals(
"Apple Inc.",
testShare.getStock().getCompany()
);
}
}

0 comments on commit e6a3914

Please sign in to comment.