'How to test custom auto-configurated beans of @RequestScope on Spring Boot?
I am creating a custom auto-configurated[1] "starter"[2] to create "request" scoped beans for Unleash in Spring Boot 2.6. I would like to write some JUnit 5 tests to verify is the beans are being created with the correct settings and if they retract according with the conditional annotations I used. I am trying to follow this post but my test cases are failing. I am already using instance of WebApplicationContextRunner - as my instance of contextRunner.
Although "testBeanName()" passes:
@Test
void testBeanName() throws Exception {
contextRunner.run(context -> {
assertThat(context).hasBean(BEAN_NAME);
});
}
@Test
void testSingleBean() throws Exception {
contextRunner.run(context -> {
assertThat(context).hasSingleBean(UnleashConfig.class);
});
}
"testSingleBean()" fails with:
java.lang.AssertionError:
Expecting:
<Started application [AnnotationConfigServletWebApplicationContext@6ffab045 id = 'org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext@6ffab045', applicationName = '', beanDefinitionCount = 16]>
to have a single bean of type:
<io.getunleash.util.UnleashConfig>
but found:
<["scopedTarget.br.gov.capes::Unleash::config", "br.gov.capes::Unleash::config"]>
@Test
void testBeanProperties() throws Exception {
contextRunner.run(context -> {
assertThat(context).getBean(UnleashConfig.class).hasFieldOrPropertyWithValue("appName", "development");
});
}
"testBeanProperties()" also fails:
java.lang.AssertionError:
Expecting:
<Started application [AnnotationConfigServletWebApplicationContext@410954b id = 'org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext@410954b', applicationName = '', beanDefinitionCount = 16]>
single bean of type:
<io.getunleash.util.UnleashConfig>
but found:
<["scopedTarget.br.gov.capes::Unleash::config", "br.gov.capes::Unleash::config"]>
@Test
void testBean() throws Exception {
contextRunner.run(context -> {
UnleashConfig unleashConfig = context.getBean(UnleashConfig.class);
assertEquals("development", unleashConfig.getAppName());
});
}
"testBean()" produces the following error:
org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.br.gov.capes::Unleash::config': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:383)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:676)
at io.getunleash.util.UnleashConfig$$EnhancerBySpringCGLIB$$1675e9f8.getAppName(<generated>)
Is there a better or more adequate way to write those JUnit tests for auto-configured beans on @RequestScope?
[1]. https://www.baeldung.com/spring-boot-custom-auto-configuration
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
