Skip to content

Commit

Permalink
replacement of deprecated MockBean annotation
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Jareš <[email protected]>
  • Loading branch information
pj892031 committed Jan 8, 2025
1 parent f089353 commit a08a7d7
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());

Expand All @@ -43,4 +44,5 @@ public MessageService messageService() {
public ApiCatalogControllerExceptionHandler apiCatalogControllerExceptionHandler() {
return new ApiCatalogControllerExceptionHandler(messageService());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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."));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -31,20 +34,22 @@ 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) {
return new StaticDefinitionControllerExceptionHandler(messageService);
}

@Bean
public StaticDefinitionController staticAPIRefreshController() {
public StaticDefinitionController staticAPIRefreshController(StaticDefinitionGenerator staticDefinitionGenerator) {
return new StaticDefinitionController(staticDefinitionGenerator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -79,25 +77,27 @@ void testInit() {
@TestConfiguration
static class TestConfig {

@MockBean
private EurekaClient eurekaClient;
@Bean
public EurekaClient eurekaClient() {
return mock(EurekaClient.class);
}

@Bean
public GatewayClient gatewayClient() {
return new GatewayClient(null);
}

@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()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -43,10 +43,10 @@ class ExtensionsLoaderTest {
@Autowired
private ApplicationEventPublisher publisher;

@MockBean
@MockitoBean
private ExtensionsLoader extensionsLoader;

@MockBean
@MockitoBean
private ExtensionConfigReader reader;

@AfterEach
Expand Down

0 comments on commit a08a7d7

Please sign in to comment.