-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added negative tests for DonationTest
- Loading branch information
MatheaGjerde
committed
Mar 3, 2026
1 parent
ae7f856
commit b0614fd
Showing
1 changed file
with
85 additions
and
9 deletions.
There are no files selected for viewing
94 changes: 85 additions & 9 deletions
94
src/test/java/edu/group5/app/model/donation/DonationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,99 @@ | ||
| package edu.group5.app.model.donation; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.sql.Timestamp; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| public class DonationTest { | ||
|
|
||
| private String expectedMessage; | ||
| private int donationId1; | ||
| private int userId1; | ||
| private int organizationId1; | ||
| private BigDecimal amount1; | ||
| private Timestamp date1; | ||
| private String paymentMethod1; | ||
| private Donation donation; | ||
|
|
||
| @BeforeEach | ||
| void setUp(){ | ||
| donationId1 = 1; | ||
| userId1 = 101; | ||
| organizationId1 = 202; | ||
| amount1 = new BigDecimal("500.0"); | ||
| date1 = new Timestamp(System.currentTimeMillis()); | ||
| paymentMethod1 = "Card"; | ||
| donation = new Donation(donationId1, userId1, | ||
| organizationId1, amount1, date1, paymentMethod1); | ||
|
|
||
| } | ||
|
|
||
| private void exceptionTest(int donationId, int userId, | ||
| int organizationId, BigDecimal amount, | ||
| Timestamp date, String paymentMethod) { | ||
| IllegalArgumentException exception = assertThrows( | ||
| IllegalArgumentException.class, () -> new Donation( | ||
| donationId, userId, organizationId, amount, date, paymentMethod) | ||
| ); | ||
| assertEquals(expectedMessage, exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testIfDonationGetsDonationValues() { | ||
|
|
||
| assertEquals(1, donation.donationId()); | ||
| assertEquals(101, donation.userId()); | ||
| assertEquals(202, donation.organizationId()); | ||
| assertEquals(new BigDecimal("500.0"), donation.amount()); | ||
| assertEquals(date1, donation.date()); | ||
| assertEquals("Card", donation.paymentMethod()); | ||
| } | ||
|
|
||
| @Test | ||
| void testIfThrowsExceptionWhenDonationIdIsNotPositive() { | ||
| expectedMessage = "Donation ID must be positive"; | ||
| exceptionTest(0, userId1, organizationId1, | ||
| amount1, date1, paymentMethod1); | ||
| } | ||
|
|
||
| @Test | ||
| void testIfThrowsExceptionWhenUserIdIsNotPositive() { | ||
| expectedMessage = "User ID must be positive"; | ||
| exceptionTest(donationId1, -1, organizationId1, | ||
| amount1, date1, paymentMethod1); | ||
| } | ||
| @Test | ||
| void testIfThrowsExceptionWhenOrganizationIdIsNotPositive() { | ||
| expectedMessage = "Organization ID must be positive"; | ||
| exceptionTest(donationId1, userId1, 0, | ||
| amount1, date1, paymentMethod1); | ||
| } | ||
| @Test | ||
| void testConstructorAndGetters() { | ||
| Timestamp now = new Timestamp(System.currentTimeMillis()); | ||
| Donation donation = new Donation(1, 101, 202, new BigDecimal("500.0"), now, "Card"); | ||
| assertEquals(1, donation.getDonationId()); | ||
| assertEquals(101, donation.getUserId()); | ||
| assertEquals(202, donation.getOrganizationId()); | ||
| assertEquals(new BigDecimal("500.0"), donation.getAmount()); | ||
| assertEquals(now, donation.getDate()); | ||
| assertEquals("Card", donation.getPaymentMethod()); | ||
| void testIfThrowsExceptionWhenAmountIsNotPositive() { | ||
| expectedMessage = "Amount must be positive and not null"; | ||
| exceptionTest(donationId1, userId1, organizationId1, | ||
| new BigDecimal("0.00"), date1, paymentMethod1); | ||
| } | ||
| @Test | ||
| void testIfThrowsExceptionWhenDateIsNull() { | ||
| expectedMessage = "Date must not be null"; | ||
| exceptionTest(donationId1, userId1, organizationId1, | ||
| amount1, null, paymentMethod1); | ||
| } | ||
| @Test | ||
| void testIfThrowsExceptionWhenPaymentMethodIsNullOrEmpty() { | ||
| expectedMessage = "Payment method must not be empty"; | ||
| exceptionTest(donationId1, userId1, organizationId1, | ||
| amount1, date1, null); | ||
| exceptionTest(donationId1, userId1, organizationId1, | ||
| amount1, date1, " "); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |