From a08a7d7558f5c99325970a079f34c8214d37b56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Jare=C5=A1?= Date: Wed, 8 Jan 2025 18:08:22 +0100 Subject: [PATCH] replacement of deprecated MockBean annotation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pavel Jareš --- ...inerRetrievalTestContextConfiguration.java | 10 ++++++---- ...piDocNotFoundTestContextConfiguration.java | 9 +++++---- ...rviceNotFoundTestContextConfiguration.java | 9 +++++---- .../controllers/api/MockControllerTest.java | 8 ++++++-- .../standalone/StandaloneInitializerTest.java | 15 ++++++--------- .../StaticAPIRefreshControllerTest.java | 7 +++++++ .../StaticApiContextConfiguration.java | 19 ++++++++++++------- .../GatewayInstanceInitializerTest.java | 18 +++++++++--------- .../apiml/extension/ExtensionsLoaderTest.java | 6 +++--- 9 files changed, 59 insertions(+), 42 deletions(-) diff --git a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/ApiCatalogControllerContainerRetrievalTestContextConfiguration.java b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/ApiCatalogControllerContainerRetrievalTestContextConfiguration.java index abf4ba310b..bd9eb8cb12 100644 --- a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/ApiCatalogControllerContainerRetrievalTestContextConfiguration.java +++ b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/ApiCatalogControllerContainerRetrievalTestContextConfiguration.java @@ -10,7 +10,6 @@ package org.zowe.apiml.apicatalog.controllers.api; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Bean; import org.zowe.apiml.apicatalog.controllers.handlers.ApiCatalogControllerExceptionHandler; import org.zowe.apiml.apicatalog.services.cached.CachedProductFamilyService; @@ -21,11 +20,13 @@ class ApiCatalogControllerContainerRetrievalTestContextConfiguration { - @MockBean - private CachedProductFamilyService cachedProductFamilyService; + @Bean + public CachedProductFamilyService cachedProductFamilyService() { + return mock(CachedProductFamilyService.class); + } @Bean - public ApiCatalogController apiCatalogController() { + public ApiCatalogController apiCatalogController(CachedProductFamilyService cachedProductFamilyService) { when(cachedProductFamilyService.getAllContainers()) .thenThrow(new NullPointerException()); @@ -43,4 +44,5 @@ public MessageService messageService() { public ApiCatalogControllerExceptionHandler apiCatalogControllerExceptionHandler() { return new ApiCatalogControllerExceptionHandler(messageService()); } + } diff --git a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/CatalogApiDocControllerApiDocNotFoundTestContextConfiguration.java b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/CatalogApiDocControllerApiDocNotFoundTestContextConfiguration.java index 7f22f25d65..ac4ec2b36e 100644 --- a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/CatalogApiDocControllerApiDocNotFoundTestContextConfiguration.java +++ b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/CatalogApiDocControllerApiDocNotFoundTestContextConfiguration.java @@ -10,7 +10,6 @@ package org.zowe.apiml.apicatalog.controllers.api; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Bean; import org.zowe.apiml.apicatalog.controllers.handlers.CatalogApiDocControllerExceptionHandler; import org.zowe.apiml.apicatalog.services.status.APIServiceStatusService; @@ -22,11 +21,13 @@ class CatalogApiDocControllerApiDocNotFoundTestContextConfiguration { - @MockBean - private APIServiceStatusService apiServiceStatusService; + @Bean + public APIServiceStatusService apiServiceStatusService() { + return mock(APIServiceStatusService.class); + } @Bean - public CatalogApiDocController catalogApiDocController() { + public CatalogApiDocController catalogApiDocController(APIServiceStatusService apiServiceStatusService) { when(apiServiceStatusService.getServiceCachedApiDocInfo("service2", "v1")) .thenThrow(new ApiDocNotFoundException("Really bad stuff happened")); diff --git a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/CatalogApiDocControllerServiceNotFoundTestContextConfiguration.java b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/CatalogApiDocControllerServiceNotFoundTestContextConfiguration.java index 565fc1fdc8..65053eb455 100644 --- a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/CatalogApiDocControllerServiceNotFoundTestContextConfiguration.java +++ b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/CatalogApiDocControllerServiceNotFoundTestContextConfiguration.java @@ -10,7 +10,6 @@ package org.zowe.apiml.apicatalog.controllers.api; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Bean; import org.zowe.apiml.apicatalog.controllers.handlers.CatalogApiDocControllerExceptionHandler; import org.zowe.apiml.apicatalog.services.status.APIServiceStatusService; @@ -22,11 +21,13 @@ class CatalogApiDocControllerServiceNotFoundTestContextConfiguration { - @MockBean - private APIServiceStatusService apiServiceStatusService; + @Bean + public APIServiceStatusService apiServiceStatusService() { + return mock(APIServiceStatusService.class); + } @Bean - public CatalogApiDocController catalogApiDocController() { + public CatalogApiDocController catalogApiDocController(APIServiceStatusService apiServiceStatusService) { when(apiServiceStatusService.getServiceCachedApiDocInfo("service1", "v1")) .thenThrow(new ServiceNotFoundException("API Documentation not retrieved, The service is running.")); diff --git a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/MockControllerTest.java b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/MockControllerTest.java index a4377f0880..310774e38d 100644 --- a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/MockControllerTest.java +++ b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/controllers/api/MockControllerTest.java @@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @@ -27,6 +26,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -72,9 +72,13 @@ void whenPostRequest() throws Exception { @Configuration @Profile("test") - @SpyBean(ExampleService.class) static class Context { + @Bean + public ExampleService exampleService() { + return spy(new ExampleService()); + } + @Bean public MockController mockController(ExampleService exampleService) { return new MockController(exampleService); diff --git a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/standalone/StandaloneInitializerTest.java b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/standalone/StandaloneInitializerTest.java index 9d9153817e..9fd0f06b09 100644 --- a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/standalone/StandaloneInitializerTest.java +++ b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/standalone/StandaloneInitializerTest.java @@ -16,7 +16,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; @@ -26,11 +25,7 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringExtension; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; @ExtendWith(SpringExtension.class) @ActiveProfiles("test") @@ -82,11 +77,13 @@ private ConfigurableApplicationContext mockContext(ApplicationReadyEvent event, @Profile("test") public static class TestConfiguration { - @MockBean - private StandaloneLoaderService standaloneLoaderService; + @Bean + public StandaloneLoaderService standaloneLoaderService() { + return mock(StandaloneLoaderService.class); + } @Bean - public StandaloneInitializer getStandaloneInitializer() { + public StandaloneInitializer getStandaloneInitializer(StandaloneLoaderService standaloneLoaderService) { return new StandaloneInitializer(standaloneLoaderService); } diff --git a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/staticapi/StaticAPIRefreshControllerTest.java b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/staticapi/StaticAPIRefreshControllerTest.java index 96571544ab..352f17bd69 100644 --- a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/staticapi/StaticAPIRefreshControllerTest.java +++ b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/staticapi/StaticAPIRefreshControllerTest.java @@ -10,6 +10,7 @@ package org.zowe.apiml.apicatalog.staticapi; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; @@ -25,6 +26,7 @@ import org.zowe.apiml.apicatalog.services.status.model.ServiceNotFoundException; import static org.hamcrest.Matchers.hasSize; +import static org.mockito.Mockito.reset; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -46,6 +48,11 @@ class StaticAPIRefreshControllerTest { @Autowired private StaticAPIService staticAPIService; + @BeforeEach + void setUp() { + reset(staticAPIService); + } + @Test void givenServiceNotFoundException_whenCallRefreshAPI_thenResponseShouldBe503WithSpecificMessage() throws Exception { when(staticAPIService.refresh()).thenThrow( diff --git a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/staticapi/StaticApiContextConfiguration.java b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/staticapi/StaticApiContextConfiguration.java index 0559baa523..128e43bfc9 100644 --- a/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/staticapi/StaticApiContextConfiguration.java +++ b/api-catalog-services/src/test/java/org/zowe/apiml/apicatalog/staticapi/StaticApiContextConfiguration.java @@ -10,15 +10,18 @@ package org.zowe.apiml.apicatalog.staticapi; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Bean; import org.zowe.apiml.message.core.MessageService; import org.zowe.apiml.message.yaml.YamlMessageService; +import static org.mockito.Mockito.mock; + public class StaticApiContextConfiguration { - @MockBean - private StaticAPIService staticAPIService; + @Bean + public StaticAPIService staticAPIService() { + return mock(StaticAPIService.class); + } @Bean public MessageService messageService() { @@ -31,12 +34,14 @@ public StaticAPIRefreshControllerExceptionHandler staticAPIRefreshControllerExce } @Bean - public StaticAPIRefreshController apiCatalogController() { + public StaticAPIRefreshController apiCatalogController(StaticAPIService staticAPIService) { return new StaticAPIRefreshController(staticAPIService); } - @MockBean - private StaticDefinitionGenerator staticDefinitionGenerator; + @Bean + public StaticDefinitionGenerator staticDefinitionGenerator() { + return mock(StaticDefinitionGenerator.class); + } @Bean public StaticDefinitionControllerExceptionHandler staticDefinitionControllerExceptionHandler(MessageService messageService) { @@ -44,7 +49,7 @@ public StaticDefinitionControllerExceptionHandler staticDefinitionControllerExce } @Bean - public StaticDefinitionController staticAPIRefreshController() { + public StaticDefinitionController staticAPIRefreshController(StaticDefinitionGenerator staticDefinitionGenerator) { return new StaticDefinitionController(staticDefinitionGenerator); } } diff --git a/apiml-common/src/test/java/org/zowe/apiml/product/gateway/GatewayInstanceInitializerTest.java b/apiml-common/src/test/java/org/zowe/apiml/product/gateway/GatewayInstanceInitializerTest.java index d772f87ebd..e1a62fe473 100644 --- a/apiml-common/src/test/java/org/zowe/apiml/product/gateway/GatewayInstanceInitializerTest.java +++ b/apiml-common/src/test/java/org/zowe/apiml/product/gateway/GatewayInstanceInitializerTest.java @@ -18,7 +18,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -29,9 +28,8 @@ import java.util.Collections; import static java.time.Duration.ofMillis; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTimeout; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ExtendWith(SpringExtension.class) @@ -79,8 +77,10 @@ void testInit() { @TestConfiguration static class TestConfig { - @MockBean - private EurekaClient eurekaClient; + @Bean + public EurekaClient eurekaClient() { + return mock(EurekaClient.class); + } @Bean public GatewayClient gatewayClient() { @@ -88,16 +88,16 @@ public GatewayClient gatewayClient() { } @Bean - public InstanceLookupExecutor instanceLookupExecutor() { + public InstanceLookupExecutor instanceLookupExecutor(EurekaClient eurekaClient) { return new InstanceLookupExecutor( eurekaClient ); } @Bean - public GatewayInstanceInitializer gatewayInstanceInitializer(ApplicationEventPublisher applicationEventPublisher) { + public GatewayInstanceInitializer gatewayInstanceInitializer(ApplicationEventPublisher applicationEventPublisher, EurekaClient eurekaClient) { return new GatewayInstanceInitializer( - instanceLookupExecutor(), + instanceLookupExecutor(eurekaClient), applicationEventPublisher, gatewayClient() ); diff --git a/apiml-extension-loader/src/test/java/org/zowe/apiml/extension/ExtensionsLoaderTest.java b/apiml-extension-loader/src/test/java/org/zowe/apiml/extension/ExtensionsLoaderTest.java index d69a6c88bf..5e363c55ad 100644 --- a/apiml-extension-loader/src/test/java/org/zowe/apiml/extension/ExtensionsLoaderTest.java +++ b/apiml-extension-loader/src/test/java/org/zowe/apiml/extension/ExtensionsLoaderTest.java @@ -18,7 +18,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationContextInitializedEvent; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -27,6 +26,7 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestContextManager; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.util.ReflectionTestUtils; import org.zowe.CustomBean; @@ -43,10 +43,10 @@ class ExtensionsLoaderTest { @Autowired private ApplicationEventPublisher publisher; - @MockBean + @MockitoBean private ExtensionsLoader extensionsLoader; - @MockBean + @MockitoBean private ExtensionConfigReader reader; @AfterEach