How to run all JUnit tests in a Spring Boot Application? - spring-boot

I am trying to generate a code coverage report of my Spring Boot application but am facing some issues with running all the existing unit tests at once.
Currently, I have quite a number of test classes (~20+) and when run individually on IntelliJ, they work fine with no issues or exceptions. However, when I create a JUnit configuration that builds and runs from "All in directory", 70-80% of my test classes fail and throws the same exception IllegalStateException: Failed to load ApplicationContext.
The stack trace seem to suggest some issues with the dependency injection. It follows this pattern:
java.lang.IllegalStateException: Failed to load ApplicationContext ...
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'foo': Injection of resource dependencies
failed; org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'bar': FactoryBean threw exception on object
creation org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'foobar': Initialization of bean failed;
I'm not sure if this has something to do with the #SpringBootTest annotation which is repeated throughout all the test classes (I'm thinking maybe it's spinning up the application / context multiple times resulting in this issue?)
I think perhaps I'm doing it wrongly or have configured some annotations wrongly? How can I run all the JUnit test classes on IntelliJ?
P.S Sorry for the lack of context as it's not a personal project and I have to hide some information, and also I'm very new to SpringBoot and JUnit.

Related

How to use both reactive and non reactive MongoDB template on Webflux Application

Currently, I'm developing a springboot webflux app for my project, where I use reactive-mongo-template and other reactive implementations. Now I have a legacy spring boot application that is implemented by WebMvc ( here i dont use the real springboot app. just using the core services implemented with mongoDB configurations) and it's using (normal) mongo-template for its implemented services. In my webflux app I'm trying to use the old services defined in the legacy application.
For that i tried importing the legacy app using component scan (com.a.b.cservice.core.configuration) and got some error. The problem is it wont detect the application correctly and the non-reactive mongo template.
Does the spring support only 1 mongo connection? These are the error logs i got;
[main] WARN o.s.b.w.r.c.AnnotationConfigReactiveWebServerApplicationContext - [] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mapperDAOSpringConfigurations': Unsatisfied dependency expressed through field 'mongoTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource
[com/a/b/cservice/core/configuration/MongoConfigurations.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: com.mongodb.connection.ClusterSettings.getDescription()Ljava/lang/String;
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.mongodb.connection.DefaultClusterFactory.createCluster(DefaultClusterFactory.java:182)
The following method did not exist:
com.mongodb.connection.ClusterSettings.getDescription()Ljava/lang/String;
The method's class, com.mongodb.connection.ClusterSettings, is available from the following locations:
jar:file:/C:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.jar!/com/mongodb/connection/ClusterSettings.class
jar:file:/C:/Users/abc/.m2/repository/org/mongodb/mongo-java-driver/3.12.7/mongo-java-driver-3.12.7.jar!/com/mongodb/connection/ClusterSettings.class
The class hierarchy was loaded from the following locations:
com.mongodb.connection.ClusterSettings: file:/C:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.ClusterSettings
Are there any workaround to work the legacy app with the webflux application with mongoDb? Or do i need to keep it as a seperate micro-service and call the relevent endpoints?
after a few works and searches, I found the issue is not related to Webflux. purely a mongo client collision between legacy application and webflux drivers. so I exclude the drivers from legacy and imported the spring-boot-starter-data-mongodb along with the reactive spring-boot-starter-data-mongodb-reactive version. (both are using the same version 2.3.5.RELEASE).
ps: found the same issue on here Spring boot 2.0.5.RELEASE and mongo 4.0 connection issues

Spring Boot application cannot join Coherence cluster on server: Join request was aborted

I have developed a small REST API application with Spring Boot after another sample application with classic Spring XML configuration. It imports context from a library with a Coherence cache set up.
When I developed it locally, running the Spring Boot app with Intellij Idea runner, there were no issues, the application worked fine and the endpoint worked connecting to that cache.
When I deployed the application to the Unix server, I am getting the following error during the application startup:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coreSearchSync' defined in class path resource [core-search-core-context.xml]: Cannot resolve reference to bean 'corePersistenceManager' while setting bean property 'corePersistenceManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'corePersistenceManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' coreSearchResultCache' defined in class path resource [core-search-coherence-context.xml]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.tangosol.net.NamedCache]: Factory method 'getCache' threw exception; nested exception is (Wrapped: Failed to start Service "Management" (ServiceState=SERVICE_STOPPED)) java.lang.RuntimeException: Join request was aborted
The application on the server is using the application.properties but they are the same as in the application.yaml locally.
I am not sure where to continue the investigation...
The problem was solved by passing JVM arguments to the script to start the application:
-Dtangosol.coherence.clusterport=<port> -Dtangosol.coherence.site=<site> -Dtangosol.coherence.clusteraddress=<cluster_ip_ddress> -Dtangosol.coherence.cluster=<cluster_name>
It appears that these properties are not applied from the application properties because starting the coherence cluster works on the system level and/or starts before the properties are initialized. Or those properties are not visible to the coherence.

How to avoid spring application failure due to BeanCreationException caused by third party package

I am using a third-party package in my spring boot app. This one to be specific. This package has a method that creates a bean when the application starts. The bean is only created successfully if this package is able to talk to nats service. In most cases, this all works fine. The remote server is available, the bean is created, my spring boot apps boots up correctly.
But there are some edge cases when remote nats servers might not be up. This causes my spring boot app to fail to start as the above function throws BeanCreationException
How can I avoid this i.e avoid my spring app boot failure due to this exception when the nats server is not up?
Note: It is fine for my spring app/business logic to be up and running if this bean (connections to nats service) is not available.
Specific Exception :
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'natsConnection' defined in class path resource [io/nats/spring/boot/autoconfigure/NatsAutoConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.nats.client.Connection]:
Factory method 'natsConnection' threw exception; nested exception is java.io.IOException: Unable to connect to NATS server.\n\tat org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)\n\tat org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)\n\tat org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)\n\tat org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)\n\tat org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)\n\tat
The bean natsConnection is created by auto-configuration and annotated with #ConditionalOnMissingBean. Such beans are instantiated after your own beans.
You can create your own bean of this type, io.nats.client.Connection, and register it as natsConnection. This bean should have no own logic. When called for the first time, it should create a worker instance via NatsAutoConfiguration.natsConnection(...) and then delegate all calls to it.
Because of #ConditionalOnMissingBean the default bean from the NATS library will not be instantiated and only your bean will be created.

java version "1.8.0_121" Requested bean is currently in creation: Is there an unresolvable circular reference?

When I build and deploy my spring based Application with
java version "1.8.0_112" it compiles and deploys fine.
Also, if i compile with java version "1.8.0_121", and deploy with
java version "1.8.0_112" that works too.
But when I compile and deploy the same Application with
java version "1.8.0_121"
it gives me an error for one service:
Error creating bean with name 'namesServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private a.b.c.backend.services.account.PersonService a.b.c.backend.services.serviceimpl.NamesServiceImpl.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CISExpressionHandler' defined in URL
[jar:file:/usr/local/tomcat7-7/webapps/ServicesApp/WEB-INF/lib/E2Services.jar!/META-INF/spring/applicationContext-security.xml]: Cannot resolve reference to bean 'CISPermissionEvaluator' while setting bean property 'permissionEvaluator'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'CISPermissionEvaluator': Requested bean is currently in creation: Is there an unresolvable circular reference?
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private a.b.c.backend.services.ciscase.CaseService a.b.c.backend.services.security.permissionEvaluators.CaseOwnerPermission.caseService; nested exception is org.springframework.beans.factory.BeanCreationException
One explanation, I could find was:
The exception may or may not occur depends on the creation order of beans. We load them with config something like below in web.xml
(/usr/local/tomcat7-7/webapps/ServicesApp/WEB-INF/web.xml)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
The XML files will be loaded by XmlWebApplicationContext class and the loading order of files are not guaranteed.
It just loads files from the file system. The problem is here.
There's no problem if the class loads the application context file first, because your beans are already created when they are used for the construction injection of Spring Security. But, if it loads the Spring Security context file first, the circular reference problem occurs, because Spring tries to use your beans in the constructor injection before they had been created.
How to solve the problem?
Force the loading order of the xml files. loading the security context xml file at the end of the application context file by using
<import resource="applicationContext-security.xml">.
Now how this got introduced with JDK version change? I don't have an explanation for that
ref: Splitting applicationContext to multiple files
Q: How do i get the order of beans at deployment ?

OAUTH2 transaction manager grails mongodb

I am working on an app that uses a mongodb, is built in grails, and is using oauth2 as an authentication service. I have the services and controller built out (I think) and am trying to run my server to manually test connectivity with google. I am running into an "Error creating bean with name transactionManagerPostProcessor..." that I can't solve. I know I need to either install a plug-in that takes care of the "transactions" for me, or create a bean in resources.groovy.
I have done a lot of researching and reading up on this, but I have been at a dead end for the last couple days trying to get this implemented. Can someone tell me how to solve this problem, create the correct bean, or install the correct plug-in? Partial stack trace below, with link to full stack trace:
|Loading Grails 2.3.4
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
............................................
|Running Grails application
Configuring Spring Security Core ...
... finished configuring Spring Security Core
Error |
2014-01-16 09:30:52,840 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatastore': Cannot resolve reference to bean 'mongoMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMappingContext': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: org/springframework/format/datetime/DateFormatterRegistrar
Full stacktrace can be found at: http://pastebin.com/5mW5uwtu
I have tried adding
static transaction = 'mongo'
to the service that calls google to authenticate the user. This is a solution I found on a similiar question on stackoverflow. I have also fiddled with different plugins and dependencies in my build-config. Not sure what else there is to try.
Thank you!
To work around this issue with some plugins, I've added the following to the beans block in resources.groovy
// some plugins need a transaction manager,
// although the mongo one isn't properly transactional
// and could produce unexpected results in some cases.
springConfig.addAlias('transactionManager', 'mongoTransactionManager')
However, the specific error you're seeing relates to the Spring class DateFormatterRegistrar not being found. Are you using an up-to-date version of Grails (e.g. 2.3+) and the mongo plugin? Have you added the mongo plugin as a compile plugin dependency in BuildConfig?:
compile ":mongodb:1.3.3"

Resources