Skip to content

Commit

Permalink
Task 34 : Add Java Doc for AbstractBaseServiceTest, AbstractRestContr…
Browse files Browse the repository at this point in the history
…ollerTest, AbstractTestContainerConfiguration and GlobalExceptionHandlerTest
  • Loading branch information
Rapter1990 committed Jul 2, 2024
1 parent f9c01df commit 7c9b957
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

/**
* Abstract base class named {@link AbstractBaseServiceTest} for service layer tests using Mockito.
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public abstract class AbstractBaseServiceTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;


/**
* Base class named {@link AbstractRestControllerTest} for REST controller tests.
*/
@SpringBootTest
@AutoConfigureMockMvc
public class AbstractRestControllerTest extends AbstractTestContainerConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

/**
* Configuration class named {@link AbstractTestContainerConfiguration} for Testcontainers setup.
*/
@Testcontainers
class AbstractTestContainerConfiguration {

Expand All @@ -18,13 +21,21 @@ class AbstractTestContainerConfiguration {
public static GenericContainer<?> redisContainer = new GenericContainer<>("redis:latest")
.withExposedPorts(6379);

/**
* Starts the Testcontainers before all tests.
*/
@BeforeAll
static void beforeAll() {
MYSQL_CONTAINER.withReuse(true);
MYSQL_CONTAINER.start();
redisContainer.start();
}

/**
* Overrides Spring properties with container properties.
*
* @param dynamicPropertyRegistry the registry to override properties
*/
@DynamicPropertySource
private static void overrideProps(DynamicPropertyRegistry dynamicPropertyRegistry) {
dynamicPropertyRegistry.add("spring.datasource.username", MYSQL_CONTAINER::getUsername);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Test class for {@link GlobalExceptionHandler}.
* This class tests the various exception handling methods of the {@link GlobalExceptionHandler} class
* to ensure they return the expected {@link CustomError} responses.
*/
class GlobalExceptionHandlerTest extends AbstractRestControllerTest {

@InjectMocks
private GlobalExceptionHandler globalExceptionHandler;

/**
* Tests the handling of {@link MethodArgumentNotValidException}.
* This test verifies that a {@link CustomError} is returned with the expected properties
* when a {@link MethodArgumentNotValidException} is thrown.
*/
@Test
void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCustomError() {

Expand Down Expand Up @@ -60,7 +70,11 @@ void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCust

}


/**
* Tests the handling of {@link ConstraintViolationException} for path variable errors.
* This test verifies that a {@link CustomError} is returned with the expected properties
* when a {@link ConstraintViolationException} is thrown.
*/
@Test
void givenConstraintViolationException_whenHandlePathVariableErrors_throwCustomError() {

Expand Down Expand Up @@ -102,6 +116,11 @@ void givenConstraintViolationException_whenHandlePathVariableErrors_throwCustomE

}

/**
* Tests the handling of {@link RuntimeException}.
* This test verifies that a {@link CustomError} is returned with the expected properties
* when a {@link RuntimeException} is thrown.
*/
@Test
void givenRuntimeException_whenHandleRuntimeException_throwCustomError() {

Expand All @@ -126,6 +145,11 @@ void givenRuntimeException_whenHandleRuntimeException_throwCustomError() {

}

/**
* Tests the handling of {@link RateLimitExceededException}.
* This test verifies that a {@link CustomError} is returned with the expected properties
* when a {@link RateLimitExceededException} is thrown.
*/
@Test
void givenRateLimitExceededException_whenHandleRateLimitExceededException_throwCustomError() {

Expand All @@ -151,6 +175,11 @@ void givenRateLimitExceededException_whenHandleRateLimitExceededException_throwC

}

/**
* Tests the handling of {@link EmailAlreadyExistsException}.
* This test verifies that a {@link CustomError} is returned with the expected properties
* when an {@link EmailAlreadyExistsException} is thrown.
*/
@Test
void givenEmailAlreadyExistsException_whenHandleEmailAlreadyExistsException_throwCustomError() {

Expand All @@ -175,6 +204,11 @@ void givenEmailAlreadyExistsException_whenHandleEmailAlreadyExistsException_thro

}

/**
* Tests the handling of {@link UserNotFoundException}.
* This test verifies that a {@link CustomError} is returned with the expected properties
* when a {@link UserNotFoundException} is thrown.
*/
@Test
void givenUserNotFoundException_whenHandleUserNotFoundException_throwCustomError() {

Expand All @@ -199,6 +233,12 @@ void givenUserNotFoundException_whenHandleUserNotFoundException_throwCustomError

}

/**
* Verifies the properties of a {@link CustomError}.
*
* @param expectedError the expected custom error
* @param actualError the actual custom error
*/
private void checkCustomError(CustomError expectedError, CustomError actualError) {

assertThat(actualError).isNotNull();
Expand Down

0 comments on commit 7c9b957

Please sign in to comment.