Errors while running tests - spring

I am trying to run my tests using mvn clean install, but these errors stop the process. I tried debugging using JUNIT but nothing shown on the console.
these are the logs :
com.datamint.service.impl.EmailServiceImplDevTest:Error creating bean with name'entityManagerFactory'defined in class path resource
[orm.xml]:BeanPostProcessor before instantiation of bean failed;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'org.springframework.security.methodSecurityMetadataSourceAdvisor':Cannot resolve reference to bean'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0':Cannot create inner bean'(inner bean)'of type[org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource]while setting constructor argument with key[0];nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'(inner bean)':Cannot create inner bean'(inner bean)'of type[org.springframework.security.access.expression.method.ExpressionBasedAnnotationAttributeFactory]while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'(inner bean)':Cannot resolve reference to bean'expressionHandler'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'expressionHandler'defined in class path resource
[spring-security.xml]:Cannot resolve reference to bean'permissionEvaluator'while setting bean property'permissionEvaluator';nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'permissionEvaluator'defined in class path resource
[spring-security.xml]:Cannot resolve reference to bean'aclService'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'aclService'defined in class path resource
[spring-security.xml]:Cannot resolve reference to bean'lookupStrategy'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'lookupStrategy'defined in class path resource
[spring-security.xml]:Cannot resolve reference to bean'aclCache'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'aclCache'defined in class path resource
[spring-security.xml]:Cannot create inner bean'org.springframework.cache.ehcache.EhCacheFactoryBean#a512c9c'of type[org.springframework.cache.ehcache.EhCacheFactoryBean]while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'org.springframework.cache.ehcache.EhCacheFactoryBean#a512c9c'defined in class path resource
[spring-security.xml]:Cannot create inner bean'org.springframework.cache.ehcache.EhCacheManagerFactoryBean#68394e81'of type[org.springframework.cache.ehcache.EhCacheManagerFactoryBean]while setting bean property'cacheManager';nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'org.springframework.cache.ehcache.EhCacheManagerFactoryBean#68394e81'defined in class path resource
[spring -security.xml]:Invocation of init method failed;nested exception is net.sf.ehcache.CacheException:Another unnamed CacheManager already exists in the same VM.Please provide unique names for each CacheManager in the config or do one of following:
What can be the problem? thanks

The cause of the problem is that with the default settings, you cannot have multiple processes sharing the same EhCache. Taken from here
CacheManager supports two creation modes: singleton and instance.
Versions of Ehcache before version 2.5 allowed any number of
CacheManagers with the same name (same configuration resource) to
exist in a JVM. Therefore, each time new CacheManager(...) was called,
a new CacheManager was created without regard to existing
CacheManagers. Calling CacheManager.create(...) returned the existing
singleton CacheManager with the configured name (if it existed) or
created the singleton based on the passed-in configuration.
Ehcache 2.5 and higher does not allow multiple CacheManagers with the
same name to exist in the same JVM. CacheManager() constructors
creating non-Singleton CacheManagers can violate this rule, causing a
NullPointerException. If your code may create multiple CacheManagers
of the same name in the same JVM, avoid this error by using the static
CacheManager.create() methods, which always return the named (or
default unnamed) CacheManager if it already exists in that JVM. If the
named (or default unnamed) CacheManager does not exist, the
CacheManager.create() methods create it.
One solution that will fix your problem (but will slow down the execution of the tests), is to add #DirtiesContext to each of your tests that loads the Spring configuration. That will force Spring to recreate all the beans upon running each test class and you'll be able to sidestep the problem.
A solution I prefer in these cases is to run the tests (that don't test caching) using a different profile, where caching is not configured. That way you don't need #DirtiesContext and therefore the execution time of the tests if much better

Related

Unable to create retry-able datasource for spring boot jdbc

I would like to add the retry feature for database connection certain number of times until the app acquires it. For the I have used the spring-retry on the DataSource but it is not working. It is throwing the following error
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcTemplate' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.class]: Unsatisfied dependency expressed through method 'jdbcTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ExistingValue must be an instance of com.zaxxer.hikari.HikariDataSource
I have seen the debug logs but those are not helpful. Here is the sample source code . kindly help
Note: the dependencies mentioned in build.gradle is required for my app. I have only extracted the retry part.
Your use-case is delaying the start of Spring Boot until your database is up. Spring actually ships with a component that does that. The DatabaseStartupValidator is that component and has existed since about Spring 1.x.
You can add it as a bean and it will wait for further bootstrapping until the database is up.
#Bean
public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) {
var dsv = new DatabaseStartupValidator();
dsv.setDataSource(dataSource);
return dsv;
}
For a more detailed explanation see this blog post of mine.

Getting exception while build in junit test execution

Task :test
Exception in thread "Thread-24" java.lang.IllegalStateException: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'traceApplicationContextRefreshedListener': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:174)
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onClose(ReactiveWebServerApplicationContext.java:93)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1023)
at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:937)
Caused by: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'traceApplicationContextRefreshedListener': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:208)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:224)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1012)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:338)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:333)
at org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth.contextRefreshed(ReactorSleuth.java:145)
at org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth.lambda$null$2(ReactorSleuth.java:124)
at reactor.core.publisher.MonoLift.subscribe(MonoLift.java:42)
at reactor.core.publisher.Mono.block(Mono.java:1174)
at reactor.ipc.netty.tcp.BlockingNettyContext.shutdown(BlockingNettyContext.java:152)
at org.springframework.boot.web.embedded.netty.NettyWebServer.stop(NettyWebServer.java:123)
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:170)
... 3 more
There could be a couple of reasons for that possibly which I can recollect looking at this issue:
Setting the right path to the JAVA_HOME environment variable, updating your PATH variable and restarting your eclipse / STS IDE whichever you're using.
The other could be multiple Tomcat instances are open at the same time when you're trying to run your JUnit test cases. You could close all of those instances and open just one to get this sorted.
Kindly cross-check these and let me know it helps!

spring-boot 1.2.0.M2 breaks app

I have a spring boot app that worked fine up until I updated from 1.2.0.M1 to 1.2.0.M2. The app will not build because of an UnsatisfiedDependencyException resulting from spring-boot RabbitAutoConfiguration:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'jmsMessagingTemplate' defined in class path resource [org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration$MessagingTemplateConfiguration.class]:
Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.amqp.rabbit.core.RabbitTemplate]: :
No qualifying bean of type [org.springframework.amqp.rabbit.core.RabbitTemplate] is defined:
expected single matching bean but found 3: looperTemplate,pingTemplate,orgRequestTemplate; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [org.springframework.amqp.rabbit.core.RabbitTemplate] is defined:
expected single matching bean but found 3: looperTemplate,pingTemplate,orgRequestTemplate
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:751) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:466) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
The problem is I do have RabbitMQ templates, but I have 3 of them and it needs a qualifier to select a single template. However this code is in spring-boot, not my code. I would prefer not to have to alter spring-boot code, but I am not sure what I should do in my code to prevent this exception.
I can see the offending jmsMessagingTemplate was added in the 1.2.0.M2 version. The naming of this is also misleading as I do not have JMS enabled in my app (although this is a RabbitMQ specific auto configuration file in spring-boot).
Any suggestions on how I can configure my code without eliminating templates or modifying spring-boot code?
I've created #1701 to track the naming issue. You should read rabbitMessagingTemplate there.
There are several auto configuration instances in Boot that requires certain types to be flagged with #Primary if they're not using the "default" name. For instance a JdbcTemplate is created for you automatically if none exists and a datasource is present. If you have more than one you should either name one dataSource or flag one of them as #Primary.
In your case, the messaging auto config for RabbitMQ expects one RabbitTemplate to be named rabbitTemplate or flag one of the three as #Primary.
This is annoying and we should do better. I've created #1702 for that.
Let me know if that works out for you. Thanks!

Separate Hibernate library for multiple web applications

I want to have two web applications that use Hibernate, Spring, and JSF 2.0. One would be the website which is available to the public and the other a content management system available on the company intranet.
I don't want to have duplicate code that accesses the database. I want to create a JAR file that contains all the database manipulation using hibernate.
I can't find any examples on the we of how this is done.
Does anyone have any helpful tips? I've managed to create the hibernate JAR file and put a test main in it and that can retrieve the data from the database. I think my main problem is how do I go about configuring all this in the web application?
thanks
After playing around with things a bit. I'm almost there
I'm getting this error when deploying to glassfish now
Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BrandService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'BrandDao' while setting bean property 'brandDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BrandDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'SessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'DataSource' while setting bean property 'dataSource'; nested .... msg.seeServerLog
I think there is not difference between JAR was used by java console application(your test main) and JAR was used by java web application .

How can i remove a singleton spring bean from ApplicationContext?

I want to develop a module control system so that every spring bean can be managed by my own LifeCycle Controller.
But I can not figure out how can I remove a singleton spring bean out of ApplicationContext.
That may be an interesting problem , can you help me to resolve ?
Removing definition does both : removing definition and destroying (removing all container references on that bean) corresponding Singleton :
((BeanDefinitionRegistry) beanFactory).removeBeanDefinition("myBean");
If you just need to remove the singleton then :
((DefaultListableBeanFactory) beanFactory).destroySingleton("myBean");
The latter way may be especially useful if you just registered singleton but haven't defined any bean definitions, i.e.
((SingletonBeanRegistry) beanFactory).registerSingleton("myBean", myBeanInstance);
You can try removing the bean definition. Get the BeanDefinitionRegistry and call removeDefinition(..)
It depends on the way you create your application, but for example in web application you can get the definition registry by:
BeanDefinitionRegistry factory =
(BeanDefinitionRegistry) applicationCtx.getAutowireCapableBeanFactory();
(the bean factory implements BeanDefinitionRegistry).
I don't know if the bean instance will be removed as well. Give it a try.

Resources