Skip to content

Fixed tests for JobOpportunityService - #60

Open
manrique0466 wants to merge 2 commits into
mainfrom
58-test-for-joboppurtuinuties
Open

Fixed tests for JobOpportunityService#60
manrique0466 wants to merge 2 commits into
mainfrom
58-test-for-joboppurtuinuties

Conversation

@manrique0466

@manrique0466 manrique0466 commented Apr 25, 2025

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Tests
    • Added comprehensive unit tests for job opportunity management, including retrieval, creation, deletion, and validation of required fields.

@manrique0466 manrique0466 linked an issue Apr 25, 2025 that may be closed by this pull request
@coderabbitai

coderabbitai Bot commented Apr 25, 2025

Copy link
Copy Markdown

Walkthrough

A new test class, JobOpportunityServiceTest, has been introduced to provide unit tests for the JobOpportunityService. The tests utilize Mockito for mocking dependencies and AssertJ for assertions. The coverage includes retrieving all job opportunities, fetching by ID, creating new job opportunities, deleting by ID, and validating required fields during registration. The tests also verify that appropriate exceptions are thrown when mandatory fields are missing, ensuring both normal and error scenarios are handled.

Changes

File(s) Change Summary
src/test/java/se/iths/java24/spring25/service/JobOpportunityServiceTest.java Added a new test class with unit tests for service methods, including validation and error handling.

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
Loading

Poem

In the warren of code, a new test hops in,
Checking each field, where bugs might begin.
With mocks and assertions, it leaps through the night,
Ensuring the service behaves just right.
If a field is missing, an error is thrown—
The rabbit ensures all the gaps are well-known! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@manrique0466
manrique0466 requested a review from Raffi02k April 25, 2025 09:37

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Test for when a job ID is not found (returning empty Optional)
  2. Test for successful job registration with all fields valid
  3. 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

📥 Commits

Reviewing files that changed from the base of the PR and between 808b203 and e3b0538.

📒 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 @BeforeEach to 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-For-JobOppurtuinuties

1 participant