-
Notifications
You must be signed in to change notification settings - Fork 2
test: add unit tests for PetEntity #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb9c24d
test: getters & setters
lindaeskilsson c4675c1
test: add tests for Pet full constructor
lindaeskilsson 1abcc50
test: add tests for onCreate timestamp initialization
lindaeskilsson fd78b6f
test: add tests for onUpdate timestamp behavior
lindaeskilsson 1373664
test: add default state tests for Pet entity
lindaeskilsson da7ff7d
rabbit fixes
lindaeskilsson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
src/test/java/org/example/vet1177/entities/PetEntityTest.java
This file contains hidden or 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,175 @@ | ||
| package org.example.vet1177.entities; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.Instant; | ||
| import java.time.LocalDate; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
|
||
| public class PetEntityTest { | ||
| private Pet pet; | ||
| private User owner; | ||
|
|
||
| @BeforeEach | ||
| void setUp(){ | ||
| owner = new User("Kalle Karlsson", "kalle.k@example.se", "hasg123", Role.OWNER); | ||
| pet = new Pet(); | ||
| } | ||
| //Getters & setters (name, species, breed, dateOfBirth, weightKg, owner) | ||
| @Test | ||
| void setName_shouldStoreAndReturnCorrectValue(){ | ||
| pet.setName("Harry"); | ||
|
|
||
| assertThat(pet.getName()).isEqualTo("Harry"); | ||
| } | ||
|
|
||
| @Test | ||
| void setSpecies_shouldStoreAndReturnCorrectValue(){ | ||
| pet.setSpecies("hund"); | ||
|
|
||
| assertThat(pet.getSpecies()).isEqualTo("hund"); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void setBreed_shouldStoreAndReturnCorrectValue(){ | ||
| pet.setBreed("Labrador"); | ||
|
|
||
| assertThat(pet.getBreed()).isEqualTo("Labrador"); | ||
| } | ||
|
|
||
| @Test | ||
| void setDateOfBirth_shouldStoreAndReturnCorrectValue(){ | ||
| LocalDate dob = LocalDate.of(2025, 12, 21); | ||
| pet.setDateOfBirth(dob); | ||
|
|
||
| assertThat(pet.getDateOfBirth()).isEqualTo(dob); | ||
| } | ||
|
|
||
| @Test | ||
| void setWeightKg_shouldStoreAndReturnCorrectValue(){ | ||
| BigDecimal weight = new BigDecimal("13.50"); | ||
| pet.setWeightKg(weight); | ||
|
|
||
| assertThat(pet.getWeightKg()).isEqualByComparingTo(weight); | ||
| } | ||
|
|
||
| @Test | ||
| void setOwner_ShouldStoreAndReturnCorrectUser(){ | ||
| pet.setOwner(owner); | ||
| assertThat(pet.getOwner()).isSameAs(owner); | ||
| } | ||
|
|
||
| //Konstruktorn med alla fält | ||
| @Test | ||
| void fullConstructor_shouldSetAllFields(){ | ||
| LocalDate dob = LocalDate.of(2025,12,23); | ||
| BigDecimal weight = new BigDecimal("12.20"); | ||
|
|
||
| Pet constructed = new Pet(owner, "Harry", "hund", "pudel", dob, weight); | ||
|
|
||
| assertThat(constructed.getOwner()).isSameAs(owner); | ||
| assertThat(constructed.getName()).isEqualTo("Harry"); | ||
| assertThat(constructed.getSpecies()).isEqualTo("hund"); | ||
| assertThat(constructed.getBreed()).isEqualTo("pudel"); | ||
| assertThat(constructed.getDateOfBirth()).isEqualTo(dob); | ||
| assertThat(constructed.getWeightKg()).isEqualByComparingTo(weight); | ||
| } | ||
|
|
||
| @Test | ||
| void fullConstructor_shouldAllowNullBreed(){ | ||
| Pet constructed = new Pet(owner, "Max", "Katt", null, LocalDate.now(), new BigDecimal("5.00")); | ||
| assertThat(constructed.getBreed()).isNull(); | ||
| } | ||
|
|
||
| //onCreate() sätter createdAt och updatedAt | ||
| @Test | ||
| void onCreate_shouldSetBothTimestampsToNonNull() { | ||
| pet.onCreate(); | ||
|
|
||
| assertThat(pet.getCreatedAt()).isNotNull(); | ||
| assertThat(pet.getUpdatedAt()).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void onCreate_shouldSetTimestampsCloseToNow() { | ||
| Instant before = Instant.now(); | ||
| pet.onCreate(); | ||
| Instant after = Instant.now(); | ||
|
|
||
| assertThat(pet.getCreatedAt()).isBetween(before, after); | ||
| assertThat(pet.getUpdatedAt()).isBetween(before, after); | ||
| } | ||
|
|
||
| //onUpdate() uppdaterar updatedAt men rör inte createdAt | ||
|
|
||
| @Test | ||
| void onUpdate_shouldRefreshUpdatedAt() { | ||
| pet.onCreate(); | ||
|
|
||
| pet.onUpdate(); | ||
|
|
||
| assertThat(pet.getUpdatedAt()).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void onUpdate_shouldNotModifyCreatedAt() { | ||
| pet.onCreate(); | ||
| Instant originalCreatedAt = pet.getCreatedAt(); | ||
|
|
||
| pet.onUpdate(); | ||
|
|
||
| assertThat(pet.getCreatedAt()).isEqualTo(originalCreatedAt); | ||
| } | ||
|
|
||
|
|
||
| //Standardvärden är null innan persist | ||
| @Test | ||
| void getId_shouldBeNullBeforePersist() { | ||
| assertThat(pet.getId()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void getName_shouldBeNullWhenNotSet() { | ||
| assertThat(pet.getName()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void getSpecies_shouldBeNullWhenNotSet() { | ||
| assertThat(pet.getSpecies()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void getBreed_shouldBeNullWhenNotSet() { | ||
| assertThat(pet.getBreed()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void getDateOfBirth_shouldBeNullWhenNotSet() { | ||
| assertThat(pet.getDateOfBirth()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void getWeightKg_shouldBeNullWhenNotSet() { | ||
| assertThat(pet.getWeightKg()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void getOwner_shouldBeNullWhenNotSet() { | ||
| assertThat(pet.getOwner()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void getCreatedAt_shouldBeNullBeforeOnCreate() { | ||
| assertThat(pet.getCreatedAt()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void getUpdatedAt_shouldBeNullBeforeOnCreate() { | ||
| assertThat(pet.getUpdatedAt()).isNull(); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.