Skip to content

test: add unit tests for PetController#196

Merged
lindaeskilsson merged 5 commits into
mainfrom
test/petControllerTest
Apr 14, 2026
Merged

test: add unit tests for PetController#196
lindaeskilsson merged 5 commits into
mainfrom
test/petControllerTest

Conversation

@lindaeskilsson

@lindaeskilsson lindaeskilsson commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Closes #145

Täcker POST /pets, GET /pets/{petId}, GET /pets/owner/{ownerId}, PUT /pets/{petId} och DELETE /pets/{petId} inklusive happy path, valideringsfel, 403 och 404.

Summary by CodeRabbit

  • Tests
    • Added comprehensive test coverage for pet management endpoints: create, read, update, delete, covering success, validation errors, permission checks, and mapped error responses (400/403/404/500).
    • Minor test file formatting cleanup to improve consistency.

@coderabbitai

coderabbitai Bot commented Apr 13, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1cb7a7ca-93ee-452b-9bef-d4150e0118c3

📥 Commits

Reviewing files that changed from the base of the PR and between 4c68a68 and a401a6b.

📒 Files selected for processing (2)
  • src/test/java/org/example/vet1177/controller/CommentControllerTest.java
  • src/test/java/org/example/vet1177/controller/PetControllerTest.java
✅ Files skipped from review due to trivial changes (1)
  • src/test/java/org/example/vet1177/controller/CommentControllerTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/test/java/org/example/vet1177/controller/PetControllerTest.java

📝 Walkthrough

Walkthrough

A new Spring MVC test class PetControllerTest was added using @WebMvcTest(PetController.class) and @Import(SecurityConfig.class). It mocks PetService and UserService, provides authentication helpers and fixtures, and adds 14 tests covering POST, GET, PUT, and DELETE success and error cases (400, 403, 404, 500).

Changes

Cohort / File(s) Summary
PetController Test Suite
src/test/java/org/example/vet1177/controller/PetControllerTest.java
New comprehensive MockMvc test class for PetController. Adds fixtures, authenticatedAs(...) and validPetRequest() helpers, and 14 tests exercising POST /pets, GET /pets/{petId}, GET /pets/owner/{ownerId}, PUT /pets/{petId}, DELETE /pets/{petId} including success responses and mapped error cases (400, 403, 404, 500). Mocks PetService and UserService, wires MockMvc and ObjectMapper.
CommentController whitespace
src/test/java/org/example/vet1177/controller/CommentControllerTest.java
Minor whitespace change (blank line inserted); no behavioral or API changes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested reviewers

  • annikaholmqvist94
  • TatjanaTrajkovic

Poem

🐰 I hopped into tests with a jubilant cheer,
MockMvc and fixtures made changes clear.
POSTs and GETs, PUTs and deletes—such a spree,
I skipped real servers and mocked what I’d see.
Hooray for green builds and fewer mystery bugs! 🥕✨

🚥 Pre-merge checks | ✅ 2 | ❌ 3

❌ Failed checks (2 warnings, 1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR adds tests for GET/PUT/DELETE endpoints that issue #145 explicitly notes do not exist in PetController, making these test cases out of scope. Remove tests for GET/PUT/DELETE endpoints (getPetById, getPetsByOwner, updatePet, deletePet) until those endpoints are added to PetController in a separate issue, per issue #145's guidance.
Docstring Coverage ⚠️ Warning Docstring coverage is 27.78% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Linked Issues check ❓ Inconclusive The PR covers required POST /pets tests (valid request, invalid request, runtime error) from issue #145, but also includes GET/PUT/DELETE endpoint tests despite the issue noting that PetController lacks these endpoints. Clarify whether GET/PUT/DELETE endpoints exist in PetController or if those tests should be removed to match issue #145's requirement that they be excluded until the endpoints are added.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly describes the main change: adding unit tests for PetController, which matches the primary objective of the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/petControllerTest

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@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: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/test/java/org/example/vet1177/controller/PetControllerTest.java`:
- Around line 104-115: Add a new test method alongside
createPet_whenForbidden_shouldReturn403 that simulates petService.createPet
throwing a RuntimeException and asserts the controller maps it to HTTP 500;
specifically, mock petService.createPet(...) to thenThrow(new
RuntimeException("...")), call mockMvc.perform(post("/pets")...) with the same
.with(authenticatedAs(vet)), .header("currentUserId", UUID.randomUUID()),
.contentType(MediaType.APPLICATION_JSON) and
.content(objectMapper.writeValueAsString(validPetRequest())), and
expect(status().isInternalServerError()) to verify the mapping.
- Line 3: Change the incorrect import for ObjectMapper in PetControllerTest:
replace the current tools.jackson.databind import with
com.fasterxml.jackson.databind.ObjectMapper so the test class
(PetControllerTest) compiles; update the import statement at the top of the file
and ensure any usages of ObjectMapper in the test remain unchanged (e.g., fields
or methods referencing ObjectMapper).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 82c26e23-51c6-41dc-84a8-e8b6651cedbc

📥 Commits

Reviewing files that changed from the base of the PR and between 77c3379 and 4c68a68.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/controller/PetControllerTest.java

Comment thread src/test/java/org/example/vet1177/controller/PetControllerTest.java
Comment thread src/test/java/org/example/vet1177/controller/PetControllerTest.java
@lindaeskilsson
lindaeskilsson merged commit f0c2d8e into main Apr 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PetControllerTest – HTTP-endpoints med MockMvc

1 participant