Add unit tests for ResponseValidator with Mockito integration

Introduced `ResponseValidatorTest` to comprehensively test the `ResponseValidator` class. Enhanced the project dependencies by updating JUnit to 5.12.2 and integrating Mockito with `mockito-junit-jupiter` 5.17.0 for mocking support. Clarified Javadoc for handling `RecordMultipleResponse` cases in `ResponseValidator`.
This commit is contained in:
2025-04-26 11:38:05 +02:00
parent 697906a669
commit 25855078f8
3 changed files with 110 additions and 4 deletions
@@ -0,0 +1,99 @@
package codes.thischwa.cf;
import codes.thischwa.cf.model.AbstractResponse;
import codes.thischwa.cf.model.RecordMultipleResponse;
import codes.thischwa.cf.model.ResponseResultInfo;
import codes.thischwa.cf.model.ResultInfo;
import java.util.Arrays;
import lombok.Getter;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class ResponseValidatorTest {
@Mock
private AbstractResponse mockResponse;
@Mock
private ResponseResultInfo mockResultInfo;
@Mock
private RecordMultipleResponse mockMultipleResponse;
private ResponseValidator validatorWithException;
private ResponseValidator validatorWithoutException;
@BeforeEach
void setUp() {
validatorWithException = new ResponseValidator(true);
validatorWithoutException = new ResponseValidator(false);
}
@Test
void validateSuccessfulResponse() {
when(mockResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
when(mockResultInfo.isSuccess()).thenReturn(true);
assertDoesNotThrow(() -> validatorWithException.validate(mockResponse, false));
}
@Test
void validateFailedResponse() {
when(mockResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
when(mockResultInfo.isSuccess()).thenReturn(false);
when(mockResultInfo.getErrors()).thenReturn(Arrays.asList("Fehler 1", "Fehler 2"));
CloudflareApiException exception = assertThrows(CloudflareApiException.class,
() -> validatorWithException.validate(mockResponse, false));
assertTrue(exception.getMessage().contains("Fehler 1"));
assertTrue(exception.getMessage().contains("Fehler 2"));
}
@Test
void validateSingleResultExpectedButMultipleFound() {
when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
when(mockResultInfo.isSuccess()).thenReturn(true);
when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(2));
CloudflareApiException exception = assertThrows(CloudflareApiException.class,
() -> validatorWithException.validate(mockMultipleResponse, true));
assertTrue(exception.getMessage().contains("Unexpected result count: 2"));
}
@Test
void validateEmptyResultWithExceptionEnabled() {
when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
when(mockResultInfo.isSuccess()).thenReturn(true);
when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(0));
assertThrows(CloudflareNotFoundException.class,
() -> validatorWithException.validate(mockMultipleResponse, false));
}
@Test
void validateEmptyResultWithExceptionDisabled() {
when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
when(mockResultInfo.isSuccess()).thenReturn(true);
when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(0));
assertDoesNotThrow(() -> validatorWithoutException.validate(mockMultipleResponse, true));
}
@Getter
private static class TestResultInfo extends ResultInfo {
private final int totalCount;
TestResultInfo(int totalCount) {
this.totalCount = totalCount;
}
}
}