Skip to content

Commit

Permalink
Merge pull request #37 from einaskoi/einar/portfolioTest
Browse files Browse the repository at this point in the history
implement test for portfolio and fix small bug in Stock class
  • Loading branch information
peretr authored Feb 25, 2026
2 parents 4b688cb + de5a0f3 commit 9f35796
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public String getCompany() {
}

public BigDecimal getSalesPrice() {
return prices.getLast();
return prices.get(prices.size() - 1);
}

public void addNewSalesPrices(BigDecimal salesPrice) {
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.ntnu.idi.idatt2003.gruppe42;

import edu.ntnu.idi.idatt2003.gruppe42.Model.Portfolio;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Share;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PortfolioTest {

@Test
public void addShareTest() {
Portfolio portfolio = new Portfolio();
Stock stock = new Stock("AAPL", "Apple", new BigDecimal("10000"));
Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("15000"));

boolean result = portfolio.addShare(share);
assertTrue(portfolio.contains(share));
}

@Test
public void removeShareTest() {
Portfolio portfolio = new Portfolio();
Stock stock = new Stock("AAPL", "Apple", new BigDecimal("10000"));
Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("15000"));

portfolio.addShare(share);
boolean result = portfolio.removeShare(share);
assertFalse(portfolio.contains(share));
}

@Test
void containsTest() {
Portfolio portfolio = new Portfolio();
assertFalse(portfolio.contains(null));
}
}

0 comments on commit 9f35796

Please sign in to comment.