I have stumbled upon one issue. I am using Spring for DI in my BDD tests. My components are Glue Scoped by marking components with #Scope("cucumber-glue"). But the issue now I am facing is this components are also needed to be used in my JUnit based tests. While trying to Autowire my components spring throws:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '<bean name>': Injection of autowired dependencies failed; nested exception is java.lang.IllegalStateException: No Scope registered for scope name 'cucumber-glue'
As per the suggestion in one of the older reported git hub issues I tried registering GlueCodeScope to glueCustomConfigurer Bean but that class is package restricted so I am unable to access it. Is there another way via which I can continue to use my components in BDD as well as JUnit tests. Note I have to use cucumber-glue scope in order to ensure my tests can run in parallel and that each scenario receives a fresh bean.
Related
My project does use Redis but my unit test mocks it. However it doesn't mock it enough because I get this stack trace snippet:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisReferenceResolver': Cannot resolve reference to bean 'redisTemplate' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'redisTemplate' available
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
It is correct. I have carefully ensured there is no redisTemplate bean available because I don't want to run redis in my unit test. But I don't know why it is trying to create the redisReferenceResolver bean.
I've turned off autoconfiguration with this:
spring:
profiles: RedisMock
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
(my test has #ActiveProfiles({"WebMvcTest","RedisMock"}) in the top)
I do have one class annotated with #RedisHash but I need that for my test.
Anyone know what might be triggering the autoconfigure?
Thanks
redisReferenceResolver is defined by Spring Data’s support for Redis-based repositories. Setting spring.data.redis.repositories.enabled to false and ensuring that you haven’t used #EnableRedisRepositories anywhere in your app will disable it.
I am building unit tests for a Spring Boot application that will call a couple of web services. One service builds a PDF and the other stores that document, and there are separate classes to call these services. I'm using IntelliJ for an IDE.
When I have a single test class to run all of my tests, things start up and work fine. When I have two separate test classes, the first class continues to work fine but when running the second I receive
ERROR [main] org.springframework.boot.SpringApplication: Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name...
Could not resolve placeholder 'PDF.form.serviceURL' in value "${PDF.form.serviceURL}"
The classes are annotated with the following, for the application context to start
#RunWith(SpringRunner.class)
#SpringBootTest(classes = Application.class)
What would cause one test class to run without issue, and an identical test class to fail to start the application context?
In this scenario, the problem was related to the JUnit configuration in IntelliJ. Without the appropriate environment options, the context would not start correctly, causing the error.
OK - problem ==> 2 Datasources (or more) will cause an error when using Springboot Session.
I have a Springboot project, with different data sources for different purposes. So within said project I would like to have Spring replace the HttpSession, with the Spring-Session module's HttpSession.
The creation of the session tables is simple if you follow the guides.
But the guides and API docs say you need JUST one data source. And so, since I have 2 or more I get the following error:
2020-07-21 21:38:59.134 ERROR 171262 --- [ restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter :
Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException.
Message: Error creating bean with name 'sessionRepositoryFilterRegistration' defined in class path resource
[org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.class]:
Unsatisfied dependency expressed through method 'sessionRepositoryFilterRegistration' parameter 1;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'javax.sql.DataSource' available:
expected single matching bean but found 2: authModelDs,secureSessionDS
So since it cannot decide which one to use, I have to manually configure it, except that the entire process leads you to a class called
org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration, which is quite literally non-configurable under the given need to set your own datasource.
I have looked a bit around, and there is no way to configure the Session Filter, once it is created to replace the datasource there too.
So I am boggled: how can I set the correct datasource for the configured Spring-Session?
Any help would be appreciated.
AHA! Soooooo
There is an annotation that you can place on the Bean creation of your datasource, to specify that such a bean is the SpringBoot session datasource.
The annotation is - #SpringSessionDataSource
It is not something that is really straight forward in the documentation at all!
Props to the following Stack Overflow answer:
Spring Boot and Spring Session: How to control the DataSource
Using Spring Boot 1.5.12, we create scoped proxies for #ConfigurationProperties beans. We do this so that we can effectively have property values that are scoped to the user/session/etc. We use a BeanFactoryPostProcessor to register a scoped proxy created by ScopedProxyUtils.createScopedProxy and change the scope of the target bean definition to our custom scope.
This worked great in Spring Boot 1.5.12. However, in Spring Boot 2, the introduction of the Binder API has made this stop working. This is because when Spring Boot binds #ConfigurationProperties in its ConfigurationPropertiesBindingPostProcessor, it uses Bindable.of(type).withExistingValue(bean) passing in a type (e.g. org.example.Foo) and the bean which is an instance of ScopedProxyFactoryBean. Bindable checks that bean is an instance of type which it's not and things blow up.
Does anyone know of a general way to accomplish this? Specifically, to replace the #ConfigurationProperties bean definition with a proxy while still allowing the properties to bind to the instance? I've considered delaying the creation of the proxy until after the target has already been bound by ConfigurationPropertiesBindingPostProcessor (i.e. in my own BeanPostProcessor). However, at this point the bean is instantiated and my proxy can only replace the bean. It can't really leave the original bean in the context as a target. So I'm struggling with how to do this using a BeanPostProcessor.
EDIT: I've created a simple example project that demonstrates the issue (with the ability to check out code that works on Spring Boot 1 and fails on Spring Boot 2).
I am attempting to convert an existing spring weblogic application to a spring boot embedded tomcat application.
There are lots of moving parts so it's hard to show any code, I'm hoping there is some general answer that might clue me in to the issue.
Under weblogic, using the spring-framework 4.3.6.RELEASE libraries, the application deploys fine. It has no problems creating the different service, repository and component beans.
However, when I migrate it to Spring Boot 1.5.1.RELEASE, I get the following error:
2017-06-21 17:08:16,402 [ERROR] SpringApplication reportFailure (815) - Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'alertEventServiceImpl': Unsatisfied dependency expressed through field 'alertEventDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'alertEventDaoImpl' defined in URL [jar:file:/Users/username/Development/source/carma-war/target/carma-war-2.0.0-SNAPSHOT.war!/WEB-INF/lib/protocol-manager-1.8.0-SNAPSHOT.jar!/org/ihc/hwcir/protocol/dao/AlertEventDaoImpl.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class org.ihc.hwcir.protocol.dao.AlertEventDaoImpl]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class org.ihc.hwcir.protocol.dao.AlertEventDaoImpl
Many of our service classes are final as they shouldn't be extended. Since there are so many that are final, I wanted to minimize the amount of code in our different libraries that we modify to make this work.
I thought because the bean creation process works under weblogic, it should work under spring boot.
Things I have tried to force not using the cglib proxies:
All implementations implement interfaces already
In beans created via xml, added <aop:scoped-proxy proxy-target-class="false"/>
In beans created through annotations, added (example for service bean)
#Service
#Scope(proxyMode = ScopedProxyMode.INTERFACE)
However, in the end, I'm perplexed as to why spring can create beans (the classes marked as final) under the weblogic container but unable to do so under the embedded tomcat spring-boot container.
Spring Boot by default uses class based proxies, which will not work with final classes/methods.
To disable this add spring.aop.proxy-target-class=false to the application.properties to enable JDK Dynamic Proxies instead of class based proxies. (And revert your modifications).
NOTE: To have everything take into account the spring.aop.proxy-target-class you might need to upgrade to Spring Boot 1.5.3 as some final patches where made to include this property in parts that were missed in previous versions.
See the following issues for more information 8434, 8869 and 8887.
I was unable to make this work using M. Deinums' answer using spring.aop.proxy-target-class=false.
What worked for me was to add in the application.properties file
spring.dao.exceptiontranslation.enabled=false
Please note that this option disables proxy creation for repositories.
And in my spring boot application configurer the annotation to handle transactions without using a proxy class.
#EnableTransactionManagement(proxyTargetClass = false)
This is using Spring Boot version 1.5.1.RELEASE.