Skip to content

test: add unit tests for document and minio components#91

Merged
SandraNelj merged 1 commit into
mainfrom
test/document
Apr 20, 2026
Merged

test: add unit tests for document and minio components#91
SandraNelj merged 1 commit into
mainfrom
test/document

Conversation

@SandraNelj

@SandraNelj SandraNelj commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

-add DocumentServiceTest for upload, delete, fetch, and error handling

-add DocumentControllerTest for file endpoints and HTTP responses

-add MinioServiceTest for storage interaction methods

-verify cleanup behavior when file operations fail

-improve test coverage for document management flow

Summary by CodeRabbit

  • Tests
    • Added comprehensive test coverage for document operations, including upload, retrieval, and deletion scenarios
    • Enhanced validation of error handling and file storage integration behavior

-add DocumentServiceTest for upload, delete, fetch, and error handling

-add DocumentControllerTest for file endpoints and HTTP responses

-add MinioServiceTest for storage interaction methods

-verify cleanup behavior when file operations fail

-improve test coverage for document management flow
@coderabbitai

coderabbitai Bot commented Apr 20, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Three new test classes are added for the document management feature: DocumentControllerTest for testing HTTP endpoints, an expanded DocumentServiceTest with comprehensive Mockito-based test coverage, and a new MinioServiceTest for testing MinIO client interactions. All changes are test-only additions totaling approximately 180 lines.

Changes

Cohort / File(s) Summary
Document Feature Tests
src/test/java/org/example/team6backend/document/controller/DocumentControllerTest.java, src/test/java/org/example/team6backend/document/service/DocumentServiceTest.java, src/test/java/org/example/team6backend/document/service/MinioServiceTest.java
New Spring MVC slice test for DocumentController with GET endpoint verification and mock service stubs. Expanded DocumentServiceTest from minimal to Mockito-based suite with upload success/failure, delete with cleanup, and lookup scenarios. New MinioServiceTest verifies MinIO client deletion calls via Reflection utilities.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 Tests hop in three swift bounds,
Controllers, services, MinIO grounds—
Mocks dance where the bytes once flew,
Now coverage blooms, clean and true! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately describes the main changeset: adding unit tests for document and MinIO components, which aligns with the three new test files added (DocumentServiceTest, DocumentControllerTest, and MinioServiceTest).

✏️ 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/document

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: 3

🧹 Nitpick comments (2)
src/test/java/org/example/team6backend/document/service/DocumentServiceTest.java (1)

44-53: Assert the Document built by the service, not a stubbed replacement.

The repository mock returns a manually created Document, so this test would still pass if uploadFile stopped setting contentType, fileSize, fileKey, or incident.

Suggested stronger assertions
-		Document document = new Document();
-		document.setFileName("test.pdf");
-
-		when(documentRepository.save(any(Document.class))).thenReturn(document);
+		when(documentRepository.save(any(Document.class))).thenAnswer(invocation -> invocation.getArgument(0));
 
 		Document result = documentService.uploadFile(file, incident);
 
 		assertEquals("test.pdf", result.getFileName());
+		assertEquals("application/pdf", result.getContentType());
+		assertEquals(100L, result.getFileSize());
+		assertSame(incident, result.getIncident());
+		assertNotNull(result.getFileKey());
+		assertTrue(result.getFileKey().endsWith("_test.pdf"));
 		verify(minioService).uploadFile(anyString(), eq(file));
 		verify(documentRepository).save(any(Document.class));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@src/test/java/org/example/team6backend/document/service/DocumentServiceTest.java`
around lines 44 - 53, The test currently asserts the stubbed Document returned
by documentRepository.save, so it won't catch if DocumentService.uploadFile
fails to populate fields; change it to capture the Document passed into
documentRepository.save (use an ArgumentCaptor<Document> or verify with an
argument slot), assert on the captured Document's fileName, contentType,
fileSize, fileKey and incident, and keep verifying minioService.uploadFile was
called; reference DocumentService.uploadFile, documentRepository.save and
minioService.uploadFile when locating the test to update.
src/test/java/org/example/team6backend/document/service/MinioServiceTest.java (1)

4-12: Assert the MinIO bucket and object, not just “any args”.

This test currently passes even if deleteFile("abc") deletes the wrong object or bucket. Capture RemoveObjectArgs and assert "test" / "abc".

Suggested test tightening
 import io.minio.MinioClient;
 import io.minio.RemoveObjectArgs;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.springframework.test.util.ReflectionTestUtils;
-import static org.mockito.ArgumentMatchers.any;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.mockito.Mockito.verify;
@@
 		minioService.deleteFile("abc");
 
-		verify(minioClient).removeObject(any(RemoveObjectArgs.class));
+		ArgumentCaptor<RemoveObjectArgs> argsCaptor = ArgumentCaptor.forClass(RemoveObjectArgs.class);
+		verify(minioClient).removeObject(argsCaptor.capture());
+
+		RemoveObjectArgs args = argsCaptor.getValue();
+		assertEquals("test", args.bucket());
+		assertEquals("abc", args.object());
 	}

Also applies to: 24-28

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@src/test/java/org/example/team6backend/document/service/MinioServiceTest.java`
around lines 4 - 12, The test uses a loose verify(any()) for removeObject;
tighten it by capturing the RemoveObjectArgs passed to the MinIO client and
asserting the bucket and object values are exactly "test" and "abc". In
MinioServiceTest, add an ArgumentCaptor<RemoveObjectArgs> (or use Mockito's
capture) when verifying the client remove call for deleteFile("abc"), then
assert captured.getBucket() equals "test" and captured.getObject() equals "abc";
apply the same capture/assert pattern for the other similar test case referenced
(lines 24-28) to ensure the correct bucket/object are being deleted.
🤖 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/team6backend/document/controller/DocumentControllerTest.java`:
- Around line 43-70: Replace the `@WithMockUser` annotations in
DocumentControllerTest tests with a manual security setup that injects a
CustomUserDetails principal into the SecurityContext so
DocumentController.getFile receives a non-null authentication principal; create
a CustomUserDetails wrapping a User (or mock) and set it as the principal of a
UsernamePasswordAuthenticationToken (authenticated) in SecurityContextHolder
before calling mockMvc.perform (mirroring the pattern used in
IncidentControllerTest), and clear/reset the SecurityContext after the test to
avoid cross-test contamination.

In
`@src/test/java/org/example/team6backend/document/service/DocumentServiceTest.java`:
- Around line 85-88: The test stubs minioService.deleteFile("abc") to throw but
never verifies it was invoked, so add an explicit verification after invoking
DocumentService.deleteFile to assert minioService.deleteFile("abc") was called
(e.g., verify(minioService).deleteFile("abc") or verify(minioService,
times(1)).deleteFile("abc")) while keeping the existing assertDoesNotThrow and
verify(documentRepository).delete(document) checks; this ensures
DocumentService.deleteFile actually attempted the MinIO deletion in the failure
path.
- Around line 62-66: The test currently verifies deleteFile was called with any
string; change it to capture the actual file key uploaded and assert deleteFile
was called with that exact key: use an ArgumentCaptor<String> (or Mockito's
capture) to capture the first argument passed to minioService.uploadFile inside
the test for documentService.uploadFile, then when verifying
minioService.deleteFile(...) assert the captured upload key equals the value
passed to deleteFile; reference the test method logic around
documentService.uploadFile(...), minioService.uploadFile(...),
documentRepository.save(...), and minioService.deleteFile(...) to locate where
to add the ArgumentCaptor and equality assertion.

---

Nitpick comments:
In
`@src/test/java/org/example/team6backend/document/service/DocumentServiceTest.java`:
- Around line 44-53: The test currently asserts the stubbed Document returned by
documentRepository.save, so it won't catch if DocumentService.uploadFile fails
to populate fields; change it to capture the Document passed into
documentRepository.save (use an ArgumentCaptor<Document> or verify with an
argument slot), assert on the captured Document's fileName, contentType,
fileSize, fileKey and incident, and keep verifying minioService.uploadFile was
called; reference DocumentService.uploadFile, documentRepository.save and
minioService.uploadFile when locating the test to update.

In
`@src/test/java/org/example/team6backend/document/service/MinioServiceTest.java`:
- Around line 4-12: The test uses a loose verify(any()) for removeObject;
tighten it by capturing the RemoveObjectArgs passed to the MinIO client and
asserting the bucket and object values are exactly "test" and "abc". In
MinioServiceTest, add an ArgumentCaptor<RemoveObjectArgs> (or use Mockito's
capture) when verifying the client remove call for deleteFile("abc"), then
assert captured.getBucket() equals "test" and captured.getObject() equals "abc";
apply the same capture/assert pattern for the other similar test case referenced
(lines 24-28) to ensure the correct bucket/object are being deleted.
🪄 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: 657bec56-ba8c-47ca-a605-77745f45b423

📥 Commits

Reviewing files that changed from the base of the PR and between 11463fa and b66d0fb.

📒 Files selected for processing (3)
  • src/test/java/org/example/team6backend/document/controller/DocumentControllerTest.java
  • src/test/java/org/example/team6backend/document/service/DocumentServiceTest.java
  • src/test/java/org/example/team6backend/document/service/MinioServiceTest.java

@SandraNelj
SandraNelj merged commit 886f665 into main Apr 20, 2026
3 checks passed
@SandraNelj
SandraNelj deleted the test/document branch April 20, 2026 08:16
@SandraNelj SandraNelj self-assigned this Apr 21, 2026
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.

1 participant