mocking beans in Configuration of a FeignClient - java

I've got a Spring Cloud FeignClient:
#FeignClient(name = "AccountSettingsClient", url = "${account.settings.service.url}", decode404 = true,
configuration = AccountSettingsClientConfig.class, fallbackFactory = AccountSettingsClientFallbackFactory.class)
public interface AccountSettingsClient {
#RequestMapping(method = RequestMethod.GET, value = "/settings/{uuid}",
headers = "User-Agent=page/okhttp", consumes = MediaType.APPLICATION_JSON_VALUE)
AccountSettings accountSettings(#PathVariable("uuid") String uuid);
}
The AccountSettingsClientConfig is:
#Configuration
#RequiredArgsConstructor
#EnableConfigurationProperties(SomeProperties.class)
#EnableFeignClients
public class AccountSettingsClientConfig {
#Bean
public RequestInterceptor oauth2FeignRequestInterceptor() {
return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), resource());
}
}
Now in a integration test I need to mock the oauth2FeignRequestInterceptor bean and it doesn't work:
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.NONE,
properties = {"account.settings.service.url=http://localhost:6565/users/accountsettings/"},
classes = { AccountSettingsClientConfig.class,
HttpMessageConvertersAutoConfiguration.class,
FeignAutoConfiguration.class, AccountSettingsClientIT.TestConfig.class })
#Slf4j
public class AccountSettingsClientIT {
#Inject
private AccountSettingsClient accountSettingsClient;
#TestConfiguration
static class TestConfig {
#Primary
#Bean
public RequestInterceptor oauth2FeignRequestInterceptor() {
return mock(RequestInterceptor.class);
}
}
}
I tried also it with #MockBean, it was the same effect.
Any ideas how to fix it?

Related

Communication between microservices using Feign throws bean could not be found

I'm trying to get my microservice java spring boot to communicate with another microservice using Feign but I'm getting this message when starting the application:
APPLICATION FAILED TO START
***************************
Description: Parameter 0 of constructor in ProductService required a bean of type ProductClientRepository' that could not be found.
Action: Consider defining a bean of type 'ProductClientRepository' in your configuration.
I don't know what could be wrong, I already checked if all the declared variables are in the project's properties and I already checked the imports, I don't know why it is saying that something is missing in the bean.
pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
SaleService:
#Service
#RequiredArgsConstructor
public class SaleService {
private final ProductService productService;
#Transactional
public Sale createSale(Sale sale) {
Set<Long> codesFromRequest = sale.getProducts().stream().map(p -> p.getCode())
.collect(Collectors.toSet());
validateProduct(codesFromRequest);
return saleRepository.save(sale);
}
public void validateProduct(Set<Long> codesFromRequest) {
List<SaleProductDTO> products = productService.findProduct(codesFromRequest);
Set<Long> returnedCodes = products.stream().map(p -> p.getCode()).collect(Collectors.toSet());
throwExceptionIf(validateSizeList(codesFromRequest, returnedCodes),
new ProductNotFoundException());
}
public boolean validateSizeList(Collection<?> codesFromRequest, Collection<?> returnedCodes) {
return codesFromRequest.size() != returnedCodes.size();
}
}
ProductService:
#Service
#Slf4j
#AllArgsConstructor
public class ProductService {
private final ProductClientRepository productRepository;
#Retryable(value = { Exception.class }, maxAttempts = 3, backoff = #Backoff(delay = 50))
public List<SaleProductDTO> findProduct(Set<Long> codes) {
Page<SaleProductDTO> resultPage;
try {
var search = SearchProductDTO
.builder()
.codes(codes)
.build();
resultPage = productRepository.getProducts(search);
} catch (FeignException f) {
return Collections.emptyList();
}
return resultPage.getContent();
}
}
ProductClientRepository:
#FeignClient(name = "product-service", url = "${ms-product.url}", configuration = ProductOAuth2FeignConfig.class)
public interface ProductClientRepository {
#GetMapping(value = "/chunk")
Page<SaleProductDTO> getProducts(#RequestBody SearchProductDTO searchDTO);
}
ProductOAuth2FeignConfig:
public class ProductOAuth2FeignConfig {
#Autowired
private ProductConfig productConfig;
#Bean
public RequestInterceptor stockOAuth2RequestInterceptor() {
return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), resource());
}
private OAuth2ProtectedResourceDetails resource() {
ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
resource.setAccessTokenUri(productConfig.getTokenUri());
resource.setClientId(productConfig.getTokenClientId());
resource.setClientSecret(productConfig.getTokenClientSecret());
resource.setGrantType(productConfig.getTokenGrantType());
resource.setScope(List.of(productConfig.getTokenScope()));
return resource;
}
}
ProductConfig:
#Data
#Configuration
#ConfigurationProperties(prefix = "ms-product")
public class ProductConfig {
private String tokenUri;
private String tokenGrantType;
private String tokenClientId;
private String tokenClientSecret;
private String tokenScope;
}
application.properties:
external.api=https://myadress.com/api
ms-product.url=${external.api}/products
ms-product.token-uri=${external.api}/products/oauth2/token
ms-product.token-grant-type=client_credentials
ms-product.token-client-id=client-id-value
ms-product.token-client-secret=secret-value
ms-product.token-scope=read
feign.client.config.default.connect-timeout=3000
feign.client.config.default.read-timeout=7000
Seems you need to add #EnableFeignClients annotation. Please refer Spring Boot OpenFeign

how to resolve Spring boot #EnableAsync and #Async problem?

my service:
#Service
public class ForgetService{
#Async
public CompletableFuture<Object> getTokenService() {
Map<String, Object> map = new HashMap<>(8);
map.put("status", ErrorEnum.TOKEN_SUSSCESS.getStatus());
map.put("message", ErrorEnum.TOKEN_SUSSCESS.getMessage());
return CompletableFuture.completedFuture(map);
}
}
my controller:
#RestController
public class ForgetController {
private final ForgetService forgetService;
#Autowired
private ForgetController(ForgetService forgetService) {
this.forgetService = forgetService;
}
#PostMapping(value = "/forget/token")
#Async
public CompletableFuture<Object> getTokenController() {
return CompletableFuture.completedFuture(forgetService.getTokenService());
}
}
spring boot application class:
#SpringBootApplication
#EnableAsync
public class ApitestApplication {
public static void main(String[] args) {
SpringApplication.run(ApitestApplication.class, args);
}
}
when i run my application, console log :
The bean 'forgetService' could not be injected as a 'com.apitest.service.ForgetService' because it is a JDK dynamic proxy that implements:
com.apitest.inf.ForgetServiceInf
Action:
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on #EnableAsync and/or #EnableCaching.
i tried setting 'spring.aop.proxy-target-class=true' in application.properties and setting '#EnableAsync(proxyTargetClass=true), but it's useless,so what's wrong? how to resolve it?
please use below approach, it might help you to fix this issue.
#Service
public class ForgetService{
#Bean("GetTokenService")
public CompletableFuture<Object> getTokenService() {
Map<String, Object> map = new HashMap<>(8);
map.put("status", ErrorEnum.TOKEN_SUSSCESS.getStatus());
map.put("message", ErrorEnum.TOKEN_SUSSCESS.getMessage());
return CompletableFuture.completedFuture(map);
}
#RestController
public class ForgetController {
private final ForgetService forgetService;
#Autowired
private ForgetController(ForgetService forgetService) {
this.forgetService = forgetService;
}
#PostMapping(value = "/forget/token")
#Async("GetTokenService")
public CompletableFuture<Object> getTokenController() {
return CompletableFuture.completedFuture(forgetService.getTokenService());
}
}

Is there any way to pass an annotation's values to my config in Spring Boot?

I have a config and it's #Import-ed by an annotation. I want the values on the annotation to be accessible by the config. Is this possible?
Config:
#Configuration
public class MyConfig
{
#Bean
public CacheManager cacheManager(net.sf.ehcache.CacheManager cacheManager)
{
//Get the values in here
return new EhCacheCacheManager(cacheManager);
}
#Bean
public EhCacheManagerFactoryBean ehcache() {
EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setShared(true);
return ehCacheManagerFactoryBean;
}
}
The annotation
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
#Import(MyConfig.class)
public #interface EnableMyCaches
{
String value() default "";
String cacheName() default "my-cache";
}
How would I get the value passed below in my config?
#SpringBootApplication
#EnableMyCaches(cacheName = "the-cache")
public class MyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceApplication.class, args);
}
}
Use simple Java reflection:
Class c = MyServiceApplication.getClass();
EnableMyCaches enableMyCaches = c.getAnnotation(EnableMyCaches.class);
String value = enableMyCaches.value();
Consider how things like #EnableConfigurationProperties are implemented.
The annotation has #Import(EnableConfigurationPropertiesImportSelector.class) which then imports ImportBeanDefinitionRegistrars. These registrars are passed annotation metadata:
public interface ImportBeanDefinitionRegistrar {
public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
}
You can then get annotation attributes from annotation metadata:
MultiValueMap<String, Object> attributes = metadata
.getAllAnnotationAttributes(
EnableMyCaches.class.getName(), false);
attributes.get("cacheName");

Java Library that uses Spring Data

i´m going to write a library that does some stuff and uses spring data.
The idea is that projects which uses this library can import this jar and use this library: MyLib.doSomeStuff().
It is possible to use Spring in this way and how can i initialize the ApplicationContext within the doSomeStuff() method, so that DI and the #Configuration Classes with the DataSources will be loaded?
public class MyLib {
#Autowired
private static SomeJpaRepository someJpaRepository;
public static void doSomeStuff(){
...init ApplicationContext....
...setup DataSources...
List<SomeEntity> someEntityList = someJpaRepository.someMethod();
}
// or
public static List<SomeEntity> getSomeEntityList() {
return someJpaRepository.finAll();
}
}
//other package
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(entityManagerFactoryRef = "gxEntityManager", transactionManagerRef = "gxTransactionManager",
basePackages = "com.gx")
public class GxConfig {
#Primary
#Bean(name = "gxDataSource")
public DataSource gxDataSource() {
DataSourceBuilder dataSourceBuilderGx = null;
//..
return dataSourceBuilderGx.build();
}
#Primary
#Bean(name = "gxEntityManager")
public LocalContainerEntityManagerFactoryBean gxEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(gxDataSource()).packages("com.gx").build();
}
#Primary
#Bean(name = "gxTransactionManager")
public PlatformTransactionManager gxTransactionManager(
#Qualifier("gxEntityManager") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
//other package
public interface SomeEntity extends JpaRepository<SomeEntity, Long>
{
SomeEntity findById(Long id);
}
If you have a root configuration class it can be as simple as
ApplicationContext context =
new AnnotationConfigApplicationContext(GxConfig.class);
Just don't do it every time you call doStuff() as creating an application context is expensive. If you library is meant to be used as a black box, I guess it's ok to have this isolated application context.
You can do something like this:
public class MyLib {
private ApplicationContext context;
public MyLib() {
context = new AnnotationConfigApplicationContext(GxConfig.class);
}
public void doStuff() {
SomeBean bean = context.getBean(SomeBean.class);
// do something with the bean
}
}

How to use #EnableTransactionManagement in combination with a StaticMethodMatcherPointcutAdvisor

Given the following service:
public interface MyService {
void method();
}
And it's implementation:
#Service
public class MyServiceImpl implements MyService {
#Transactional
#CustomAnnotation
#Override
public void method() {
...
}
}
I would like to use a StaticMethodMatcherPointcutAdvisor in the following manner:
public class MyPointcutAdvisor extends StaticMethodMatcherPointcutAdvisor {
...
#Override
public boolean matches(Method method, Class targetClass) {
Method m = method;
if(annotationPresent(method)) {
return true;
}
Class<?> userClass = ClassUtils.getUserClass(targetClass);
Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass);
specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
if(annotationPresent(specificMethod )) {
return true;
}
return false;
}
...
}
The problem is that Spring uses an InfrastructureAdvisorAutoProxyCreator to create a Proxy of that class, whereas the DefaultAdvisorAutoProxyCreator would create the proxy for the MyPointcutAdvisor, but the MyPointcutAdvisor is only given the proxy as targetClass parameter. Thus, the PointcutAdvisor cannot find the annotation and therefore does not match.
For completion this is my Configuration-class:
#Configuration
#EnableTransactionManagement
public class MyConfiguration {
#Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
return new DefaultAdvisorAutoProxyCreator();
}
#Bean
public MyPointcutAdvisor myPointcutAdvisor() {
return new MyPointcutAdvisor();
}
...
}
My question is: Is there a way to use #EnableTransactionManagement in combination with a StaticMethodMatcherPointcutAdvisor ?
Workarounds:
Put #CustomAnnotation into the service interface: I want to have clean interfaces.
Add #Role(BeanDefinition.ROLE_INFRASTRUCTURE) to MyPointCutAdvisor bean configuration, thus, the InfrastructureAdvisorAutoProxyCreator will create the proxy. This seems like the wrong way, since this bean is not infrastructure
Copy the beans from ProxyTransactionManagementConfiguration, remove #EnableTransactionManagement and remove #Role(BeanDefinition.ROLE_INFRASTRUCTURE), thus the DefaultAdvisorAutoProxyCreator will create the proxy, which is my current workaround and results in the following configuration:
#Configuration
public class MyWorkaroundConfiguration {
#Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
return new DefaultAdvisorAutoProxyCreator();
}
#Bean
public MyPointcutAdvisor myPointcutAdvisor() {
return new MyPointcutAdvisor();
}
#Bean
public TransactionAttributeSource transactionAttributeSource() {
return new AnnotationTransactionAttributeSource();
}
#Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
TransactionInterceptor transactionInterceptor) {
BeanFactoryTransactionAttributeSourceAdvisor advisor =
new BeanFactoryTransactionAttributeSourceAdvisor();
advisor.setTransactionAttributeSource(transactionAttributeSource());
advisor.setAdvice(transactionInterceptor);
return advisor;
}
#Bean
public TransactionInterceptor transactionInterceptor(
PlatformTransactionManager transactionManager) {
TransactionInterceptor interceptor = new TransactionInterceptor();
interceptor.setTransactionAttributeSource(transactionAttributeSource());
interceptor.setTransactionManager(transactionManager);
return interceptor;
}
...
}
Using #EnableAspectJAutoProxy instead of the DefaultAutoProxyCreator works for me.
#Configuration
#EnableAspectJAutoProxy
#EnableTransactionManagement
public class MyConfiguration {
}
This also allows using #Aspect like M. Deinum suggested.

Categories