Skip to content
Snippets Groups Projects
Commit 1cd18afa authored by Andreas's avatar Andreas
Browse files

Made tests for ItemOverview class

parent 439388de
No related branches found
No related tags found
1 merge request!4ItemOverview class (with test class)
Pipeline #202377 passed
......@@ -39,6 +39,9 @@ public class ItemOverview {
* @param newIncome The Income you want to add.
*/
public void addIncome(Income newIncome){
if(income.contains(newIncome)){
throw new IllegalArgumentException("This income is already registered");
}
income.add(newIncome);
}
......@@ -47,6 +50,9 @@ public class ItemOverview {
* @param newExpense The Expense you want to add.
*/
public void addExpense(Expense newExpense){
if(expense.contains(newExpense)){
throw new IllegalArgumentException("This expense is already registered");
}
expense.add(newExpense);
}
......
package no.ntnu.idatt1002.demo.data;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ItemOverviewTest {
ItemOverview itemOverview;
@BeforeEach
public void makeItemOverview(){
itemOverview = new ItemOverview();
}
@Test
@DisplayName("addIncome method throws exception when it should")
void addIncomeThrows(){
itemOverview.addIncome(new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
itemOverview.addIncome(new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
}
@Test
@DisplayName("addExpense method throws exception when it should")
void addExpenseThrows(){
itemOverview.addExpense(new Expense("description", 59.9f, false, ExpenseCategory.BOOKS, "03.03.23"));
itemOverview.addExpense(new Expense("description", 59.9f, false, ExpenseCategory.BOOKS, "03.03.23"));
}
@Test
@DisplayName("getTotalIncome method gives correct amount")
void getTotalIncomeCorrectAmount(){
itemOverview.addIncome(new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"));
itemOverview.addIncome(new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"));
itemOverview.addIncome(new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"));
double totalIncome = 59.9f + 62.4f + 9.81f;
assertEquals(Math.round(itemOverview.getTotalIncome()), Math.round(totalIncome));
}
@Test
@DisplayName("getTotalExpense method gives correct amount")
void getTotalExpenseCorrectAmount(){
itemOverview.addExpense(new Expense("description1", 59.9f, false, ExpenseCategory.BOOKS, "03.03.23"));
itemOverview.addExpense(new Expense("description2", 78.2f, true, ExpenseCategory.FOOD, "03.04.23"));
itemOverview.addExpense(new Expense("description3", 103.5f, true, ExpenseCategory.OTHER, "07.03.23"));
double totalExpense = 59.9f + 78.2f + 103.5f;
assertEquals(Math.round(itemOverview.getTotalExpense()), Math.round(totalExpense));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment