-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add controller test unit testingfile
- Loading branch information
Showing
1 changed file
with
59 additions
and
62 deletions.
There are no files selected for viewing
121 changes: 59 additions & 62 deletions
121
...ava/org/sagebionetworks/openchallenges/image/service/exception/ControllerAdvisorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,59 @@ | ||
// package org.sagebionetworks.openchallenges.image.service.exception; | ||
|
||
// import static org.assertj.core.api.Assertions.assertThat; | ||
// import static org.mockito.Mockito.when; | ||
|
||
// import org.junit.jupiter.api.Test; | ||
// import org.mockito.InjectMocks; | ||
// import org.mockito.Mock; | ||
// import org.mockito.MockitoAnnotations; | ||
// import org.springframework.beans.factory.annotation.Autowired; | ||
// import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
// import org.springframework.http.HttpStatus; | ||
// import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
// import org.springframework.test.web.servlet.MockMvc; | ||
// import org.springframework.validation.BindException; | ||
// import org.springframework.validation.BindingResult; | ||
// import org.springframework.validation.FieldError; | ||
|
||
// @SpringJUnitConfig | ||
// @WebMvcTest(ControllerAdvisor.class) | ||
// public class ControllerAdvisorTest { | ||
|
||
// @Autowired private MockMvc mockMvc; | ||
|
||
// @Mock private BindException bindException; | ||
|
||
// @InjectMocks private ControllerAdvisor controllerAdvisor; | ||
|
||
// @Test | ||
// public void bindException_ShouldReturnResultAndException_WhenMockErrorIsPassed() { | ||
// MockitoAnnotations.openMocks(this); | ||
|
||
// // String bindException = "There is an error"; | ||
// String header = "Exception header"; | ||
// String status = HttpStatus.BAD_REQUEST; | ||
// String webRequest = "Exception web request"; | ||
|
||
// // Create a mock FieldError | ||
// FieldError fieldError = | ||
// new FieldError( | ||
// "objectName", | ||
// "fieldName", | ||
// "rejectedValue", | ||
// false, | ||
// new String[] {"errorCode"}, | ||
// null, | ||
// "defaultMessage"); | ||
|
||
// // Create a mock BindingResult | ||
// BindingResult bindingResult = new BindException(new Object(), "objectName"); | ||
// bindingResult.addError(fieldError); | ||
// BindingResult handleBindException = | ||
// new HandleBindException(bindException, header, status, webRequest); | ||
|
||
// // // Set up the mock BindException to return the mock BindingResult | ||
// when(bindException.getBindingResult()).thenReturn(bindingResult); | ||
// when(bindException.handleBindException()).thenReturn(handleBindException); | ||
|
||
// assertThat(bindException.getBindingResult()).isEqualTo(bindingResult); | ||
// assertThat(bindException.handleBindException()).isEqualTo(handleBindException.get(0)); | ||
// } | ||
// } | ||
package org.sagebionetworks.openchallenges.image.service.exception; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
import org.sagebionetworks.openchallenges.image.service.exception.GlobalExceptionHandler; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.validation.BindException; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
import org.sagebionetworks.openchallenges.image.service.model.dto.BasicErrorDto; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindException; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ControllerAdvisorTest { | ||
|
||
@Test | ||
public void testHandleBindException() { | ||
// Create a sample BindException | ||
BindException bindException = mock(BindException.class); | ||
BindingResult bindingResult = mock(BindingResult.class); | ||
FieldError fieldError = new FieldError("objectName", "fieldName", "rejectedValue", false, null, null, "error message"); | ||
List<FieldError> fieldErrors = new ArrayList<>(); | ||
fieldErrors.add(fieldError); | ||
|
||
// Mock the behavior of the BindException and BindingResult | ||
when(bindException.getBindingResult()).thenReturn(bindingResult); | ||
when(bindingResult.getFieldErrors()).thenReturn(fieldErrors); | ||
|
||
// Create the ControllerAdvisor instance | ||
ControllerAdvisor controllerAdvisor = new ControllerAdvisor(); | ||
|
||
// Call handleBindException | ||
ResponseEntity<Object> responseEntity = controllerAdvisor.handleBindException(bindException, new HttpHeaders(), HttpStatus.BAD_REQUEST, mock(WebRequest.class)); | ||
|
||
// Verify the response | ||
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); | ||
BasicErrorDto errorDto = (BasicErrorDto) responseEntity.getBody(); | ||
} | ||
} |