Skip to content

Commit 678a57c

Browse files
committed
test created for ControllerAdvice as well, and java doc generated
1 parent 5307c2d commit 678a57c

File tree

166 files changed

+1914
-257
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+1914
-257
lines changed

government-service/src/main/java/com/csaba79coder/bestprotocol/controller/exception/ControllerExceptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ResponseEntity<Object> handleInvalidInputException(InputMismatchException
4949
@param message The error message.
5050
@return The response body as a string.
5151
*/
52-
private String responseBodyWithMessage(ErrorCode code, String message) {
52+
public String responseBodyWithMessage(ErrorCode code, String message) {
5353
return Map.of(code, message).toString();
5454
}
5555
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.csaba79coder.bestprotocol.controller.exception;
2+
3+
import com.csaba79coder.bestprotocol.controller.value.ErrorCode;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
9+
import java.util.InputMismatchException;
10+
import java.util.Map;
11+
import java.util.NoSuchElementException;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
16+
17+
/**
18+
* This class contains test methods for the {@link ControllerExceptionHandler} class.
19+
* It tests the exception handling logic for the {@link NoSuchElementException} and
20+
* {@link InputMismatchException} exceptions thrown in the controller.
21+
*/
22+
class ControllerExceptionHandlerTest {
23+
24+
private static final String ERROR_CODE_001 = "ERROR_CODE_001";
25+
private static final String ERROR_CODE_002 = "ERROR_CODE_002";
26+
27+
private final ControllerExceptionHandler handler = new ControllerExceptionHandler();
28+
29+
/**
30+
* Test case for verifying that a {@link NoSuchElementException} exception thrown in the controller
31+
* is handled correctly by the {@link ControllerExceptionHandler}.
32+
*
33+
* <p>This test method verifies that the controller handler returns an HTTP NOT_FOUND response
34+
* and the error message is correctly included in the response body.</p>
35+
*/
36+
@Test
37+
@DisplayName("handle Invalid Input Exception returns Bad Request Response")
38+
void handleNoSuchElementException_returnsNotFoundResponse() {
39+
// Given
40+
String message = "No such element found";
41+
NoSuchElementException ex = new NoSuchElementException(message);
42+
43+
// When
44+
ResponseEntity<Object> response = handler.handleNoSuchElementException(ex);
45+
46+
// Then
47+
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
48+
assertEquals(Map.of(ERROR_CODE_001, message).toString(), response.getBody());
49+
}
50+
51+
/**
52+
* Test case for verifying that an {@link InputMismatchException} exception thrown in the controller
53+
* is handled correctly by the {@link ControllerExceptionHandler}.
54+
*
55+
* <p>This test method verifies that the controller handler returns an HTTP BAD_REQUEST response
56+
* and the error message is correctly included in the response body.</p>
57+
*/
58+
@Test
59+
void handleInvalidInputException_returnsBadRequestResponse() {
60+
// Given
61+
String message = "Invalid input";
62+
InputMismatchException ex = new InputMismatchException(message);
63+
64+
// When
65+
ResponseEntity<Object> response = handler.handleInvalidInputException(ex);
66+
67+
// Then
68+
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
69+
assertEquals(Map.of(ERROR_CODE_002, message).toString(), response.getBody());
70+
}
71+
72+
/**
73+
* Test case for verifying that the {@link ControllerExceptionHandler#responseBodyWithMessage}
74+
* method correctly creates the response body with the given error code and message.
75+
*
76+
* <p>This test method verifies that the response body created by the method matches the expected
77+
* response body for a given error code and message.</p>
78+
*
79+
* private method was set public meanwhile testing it, so it can be accessed from the test class
80+
* after that set back to private and the test still works
81+
*/
82+
@Test
83+
@DisplayName("response Body With Message returns correct response body")
84+
public void testResponseBodyWithMessage() {
85+
// Test setup
86+
ControllerExceptionHandler handler = new ControllerExceptionHandler();
87+
ErrorCode errorCode = ErrorCode.ERROR_CODE_001;
88+
String errorMessage = "Test error message";
89+
90+
// Method invocation
91+
String responseBody = handler.responseBodyWithMessage(errorCode, errorMessage);
92+
93+
// Assertion
94+
assertThat(responseBody)
95+
.isEqualTo(Map.of(errorCode, errorMessage).toString());
96+
}
97+
}

0 commit comments

Comments
 (0)