/* * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.iluwatar.health.check.AsynchronousHealthChecker; import com.iluwatar.health.check.MemoryHealthIndicator; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.function.Supplier; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Status; /** * Unit tests for {@link MemoryHealthIndicator}. * */ @ExtendWith(MockitoExtension.class) class MemoryHealthIndicatorTest { /** Mocked AsynchronousHealthChecker instance. */ @Mock private AsynchronousHealthChecker asynchronousHealthChecker; /** `MemoryHealthIndicator` instance to be tested. */ @InjectMocks private MemoryHealthIndicator memoryHealthIndicator; /** * Test case for the `health()` method when memory usage is below the threshold. * *
Asserts that when the `health()` method is called and memory usage is below the threshold,
* it returns a Health object with Status.UP.
*/
@Test
void whenMemoryUsageIsBelowThreshold_thenHealthIsUp() {
// Arrange
CompletableFuture Asserts that when the `health()` method is called and memory usage is above the threshold,
* it returns a Health object with Status.DOWN.
*/
@Test
void whenMemoryUsageIsAboveThreshold_thenHealthIsDown() {
// Arrange
CompletableFuture Asserts that when the `health()` method is called and the health check is interrupted, it
* returns a Health object with Status DOWN and an error detail indicating the interruption.
*
* @throws ExecutionException if the future fails to complete
* @throws InterruptedException if the thread is interrupted while waiting for the future to
* complete
*/
@Test
void whenHealthCheckIsInterrupted_thenHealthIsDown()
throws ExecutionException, InterruptedException {
// Arrange
CompletableFuture Asserts that when the `health()` method is called and the health check execution fails, it
* returns a Health object with Status DOWN and an error detail indicating the failure.
*/
@Test
void whenHealthCheckExecutionFails_thenHealthIsDown() {
// Arrange
CompletableFuture