-
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.
Feat: Added UnitTest for CharityRegistry class
- Loading branch information
AdrianBalunan
committed
Mar 4, 2026
1 parent
7e62703
commit 30c5e48
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...ehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityRegistryTest.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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package ntnu.systemutvikling.team6.models; | ||
|
|
||
| import ntnu.sytemutvikling.team6.models.Charity; | ||
| import ntnu.sytemutvikling.team6.models.CharityRegistry; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class CharityRegistryTest { | ||
| private CharityRegistry registry; | ||
| private Charity charity; | ||
|
|
||
| @BeforeEach | ||
| public void setup(){ | ||
| registry = new CharityRegistry(); | ||
| charity = new Charity("Charity1", "Something Somewhere Somehow", "Cancer"); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void testAddCharitySuccessfully() { | ||
| registry.addCharity(charity); | ||
|
|
||
| assertEquals(1, registry.getAllCharities().size()); | ||
| assertTrue(registry.getAllCharities().contains(charity)); | ||
| } | ||
|
|
||
| @Test | ||
| void testAddCharityNullThrowsException() { | ||
| assertThrows(IllegalArgumentException.class, () -> registry.addCharity(null)); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetAllCharitiesReturnsUnmodifiableList() { | ||
| registry.addCharity(charity); | ||
|
|
||
| List<Charity> charities = registry.getAllCharities(); | ||
| assertThrows(UnsupportedOperationException.class, () -> charities.add(charity)); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindCharityByIdFound() { | ||
| registry.addCharity(charity); | ||
|
|
||
| Optional<Charity> result = registry.findCharityById(charity.getId()); | ||
|
|
||
| assertTrue(result.isPresent()); | ||
| assertEquals(charity, result.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindCharityByIdNotFound() { | ||
| Optional<Charity> result = registry.findCharityById(UUID.randomUUID()); | ||
| assertTrue(result.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void testFindCharityByIdNullThrowsException() { | ||
| assertThrows(IllegalArgumentException.class, () -> registry.findCharityById(null)); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemoveCharitySuccessfully() { | ||
| registry.addCharity(charity); | ||
|
|
||
| boolean removed = registry.removeCharity(charity.getId()); | ||
|
|
||
| assertTrue(removed); | ||
| assertTrue(registry.getAllCharities().isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemoveCharityNotFound() { | ||
| boolean removed = registry.removeCharity(UUID.randomUUID()); | ||
| assertFalse(removed); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemoveCharityNullThrowsException() { | ||
| assertThrows(IllegalArgumentException.class, () -> registry.removeCharity(null)); | ||
| } | ||
| } |