Skip to content

Commit

Permalink
Update SaleTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 25, 2026
1 parent d34b9a2 commit 76f784d
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/model/SaleTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import edu.ntnu.idi.idatt2003.g40.mappe.service.SaleCalculator;
import java.math.BigDecimal;

import edu.ntnu.idi.idatt2003.g40.mappe.service.PurchaseCalculator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -14,44 +17,70 @@
final class SaleTest {

/**
* Valid test stock.
* Test sale used in various tests.
* */
private final Stock testStock =
new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00"));
private Sale testSale;

/**
* Valid test share.
* */
private final Share testShare =
new Share(testStock, new BigDecimal("10"), new BigDecimal("10"));
private Share testShare;

/**
* Valid test purchase calculator.
* */
private final PurchaseCalculator testSaleCalculator =
new PurchaseCalculator(testShare);
private SaleCalculator testSaleCalculator;

/**
* Valid test player.
* */
private final Player testPlayer =
new Player("TestName", new BigDecimal("1000.00"));
private Player testPlayer;

@BeforeEach
void setUp() {
Stock testStock = new Stock(
"AAPL",
"Apple Inc.",
new BigDecimal("100.00")
);
testShare = new Share(
testStock,
new BigDecimal("10"),
new BigDecimal("10")
);
testSaleCalculator = new SaleCalculator(testShare);
testPlayer = new Player("TestName", new BigDecimal("1000.00"));
testSale = new Sale(testShare, 1, testSaleCalculator);
}

@Test
void constructorSetsValues() {
Sale sale = new Sale(testShare, 1, testSaleCalculator);

assertEquals(testShare, sale.getShare());
assertEquals(1, sale.getWeek());
assertEquals(testSaleCalculator, sale.getCalculator());
assertEquals(testShare, testSale.getShare());
assertEquals(1, testSale.getWeek());
assertEquals(testSaleCalculator, testSale.getCalculator());
}

@Test
void commitMethodSetsCommitToTrue() {
Sale sale = new Sale(testShare, 1, testSaleCalculator);
void constructorThrowsExceptionOnIllegalArguments() {
assertDoesNotThrow(
() -> new Purchase(testShare, 1, testSaleCalculator)
);

sale.commit(testPlayer);
assertThrows(IllegalArgumentException.class,
() -> new Purchase(null, 1, testSaleCalculator)
);
assertThrows(IllegalArgumentException.class,
() -> new Purchase(testShare, 0, testSaleCalculator)
);
assertThrows(IllegalArgumentException.class,
() -> new Purchase(testShare, 1, null)
);
}

assertTrue(sale.isCommited());
@Test
void commitMethodSetsCommitToTrue() {
assertFalse(testSale.isCommited());
testSale.commit(testPlayer);
assertTrue(testSale.isCommited());
}
}

0 comments on commit 76f784d

Please sign in to comment.