Fixed tests for JobOpportunityService - #60
Conversation
WalkthroughA new test class, Changes
Sequence Diagram(s)sequenceDiagram
participant Test
participant Service as JobOpportunityService
participant Repo as JobOpportunityRepository
Test->>Service: getAllJobOpportunities()
Service->>Repo: findAll()
Repo-->>Service: List<JobOpportunity>
Service-->>Test: List<JobOpportunity>
Test->>Service: getJobOpportunityById(id)
Service->>Repo: findById(id)
Repo-->>Service: Optional<JobOpportunity>
Service-->>Test: Optional<JobOpportunity>
Test->>Service: createJobOpportunity(job)
Service->>Repo: save(job)
Repo-->>Service: JobOpportunity
Service-->>Test: JobOpportunity
Test->>Service: deleteJobOpportunity(id)
Service->>Repo: deleteById(id)
Repo-->>Service: void
Service-->>Test: void
Test->>Service: registerJob(job)
alt Missing required field
Service-->>Test: throws IllegalArgumentException
else All fields valid
Service->>Repo: save(job)
Repo-->>Service: JobOpportunity
Service-->>Test: JobOpportunity
end
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/test/java/se/iths/java24/spring25/service/JobOpportunityServiceTest.java (3)
38-50: Non-English comments should be translated for consistency.The test logic is correct, but there are comments in Swedish (lines 42, 45, and 48) while the rest of the codebase appears to be in English.
- // Mockar repository så att findById returnerar ett Optional med vårt jobb + // Mock repository to return an Optional with our job - // Anrop till service-metoden + // Call to the service method - // Verifiera att det returnerade resultatet inte är tomt och innehåller vårt jobb + // Verify that the returned result is not empty and contains our job
15-104: Consider adding missing test scenarios.While the test coverage is good, consider adding these additional scenarios:
- Test for when a job ID is not found (returning empty Optional)
- Test for successful job registration with all fields valid
- Test for when the repository throws an exception
Example implementation for the "job not found" scenario:
@Test void shouldReturnEmptyOptionalWhenJobNotFound() { when(repository.findById(99L)).thenReturn(Optional.empty()); Optional<JobOpportunityEntity> result = service.getJobOpportunityById(99L); assertThat(result).isEmpty(); }Example implementation for successful job registration:
@Test void shouldRegisterNewJobSuccessfully() { // Arrange String company = "Company"; String title = "Title"; String description = "Desc"; String location = "Loc"; String terms = "Terms"; JobOpportunityEntity jobEntity = new JobOpportunityEntity(company, title, description, location, terms); when(repository.save(any(JobOpportunityEntity.class))).thenReturn(jobEntity); // Act JobOpportunityEntity result = service.registerNewJob(company, title, description, location, terms); // Assert assertThat(result).isNotNull(); assertThat(result.getCompanyName()).isEqualTo(company); assertThat(result.getJobTitle()).isEqualTo(title); assertThat(result.getJobDescription()).isEqualTo(description); assertThat(result.getLocation()).isEqualTo(location); assertThat(result.getTermsOfEmployment()).isEqualTo(terms); verify(repository).save(any(JobOpportunityEntity.class)); }
72-102: Inconsistent capitalization in error message checks.There's inconsistency in the capitalization within error message checks:
- Line 74: "Name" (capitalized)
- Line 81: "Jobtitle" (single word)
- Line 88: "Job description" (not capitalized)
- Line 95: "Location" (capitalized)
- Line 102: "Terms Of Employment" (title case)
Consider standardizing the capitalization pattern in both the error messages and the assertions.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/test/java/se/iths/java24/spring25/service/JobOpportunityServiceTest.java(1 hunks)
🔇 Additional comments (6)
src/test/java/se/iths/java24/spring25/service/JobOpportunityServiceTest.java (6)
1-14: Good organization of imports.The imports are well-organized and include all necessary dependencies for testing (JUnit, AssertJ, Mockito) and application classes.
15-24: Good test setup with proper dependency mocking.The test class correctly uses
@BeforeEachto set up the test environment, mocking the repository dependency and injecting it into the service. This approach follows best practices for unit testing.
26-36: LGTM: Comprehensive test for retrieving all job opportunities.This test appropriately verifies that the service returns all job opportunities from the repository. The assertions check both the collection size and that it contains the expected elements.
52-62: LGTM: Test for saving a new job opportunity is well-structured.This test correctly verifies that the service forwards the job entity to the repository and returns the saved entity.
64-68: LGTM: Concise and effective test for deletion.The test efficiently verifies that the service delegates the delete operation to the repository with the correct ID.
70-103: Good test coverage for validation scenarios.The validation tests thoroughly cover all required fields and verify appropriate exceptions are thrown with relevant messages.
Summary by CodeRabbit