Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test auditing with scope request bean fail #23822

Closed
guillaumeyan opened this issue Oct 23, 2020 · 2 comments
Closed

Test auditing with scope request bean fail #23822

guillaumeyan opened this issue Oct 23, 2020 · 2 comments
Labels
for: external-project For an external project and not something we can fix

Comments

@guillaumeyan
Copy link

I have a DataJpaTest that failed when running after another test annotated with SpringBootTest.

The test with DataJpaTest run correctly when started alone.

I am using auditing with a scope request bean and this is the error message :

Error creating bean with name 'scopedTarget.user': 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.

But if I understood well, the context from Spring should be another one when launching the DataJpaTest ?

Here is the demo of the bug :

https://github.com/guillaumeyan/bugspringtest

Launch all tests, but start with AppTest as first.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 23, 2020
@wilkinsona
Copy link
Member

Thanks for the sample. Unfortunately, this is another problem caused by a known issue with Spring Framework's testing support where a static aspect causes some cross-context pollution. Please see spring-projects/spring-framework#20775 for details, along with the older issues that it references.

You may be able to work around the problem with a context customizer:

package com.guillaumeyan.bugspringtest;

import java.util.List;

import org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration;

final class AspectResetContextCustomizerFactory implements ContextCustomizerFactory {
	
	private static final AspectResetContextCustomizer customizer = new AspectResetContextCustomizer();

	@Override
	public ContextCustomizer createContextCustomizer(Class<?> testClass,
			List<ContextConfigurationAttributes> configAttributes) {
		return customizer;
	}
	
	static class AspectResetContextCustomizer implements ContextCustomizer {

		@Override
		public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
			ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
			AnnotationBeanConfigurerAspect.aspectOf().setBeanFactory(beanFactory);
		}
		
	}

}

It needs to be registered in a META-INF/spring.factories file in src/test/resources:

org.springframework.test.context.ContextCustomizerFactory=com.guillaumeyan.bugspringtest.AspectResetContextCustomizerFactory

The workaround in 20775 does not work in this case as the bean factory needs to be set on the aspect before the context is refreshed. This requires hooking into the creation of the context so that the bean factory can be retrieved from the context prior to refresh.

@sbrannen we've seen this limitation of the test framework cause problems for Boot users a few times now. spring-projects/spring-framework#20775 has been closed as a duplicate of spring-projects/spring-framework#11019 and spring-projects/spring-framework#10789, one of which is waiting for triage and the other is an enhancement. FWIW, the limitation really feels like a bug to me. What are the prospects of it being resolved in a 5.x maintenance release?

@wilkinsona wilkinsona added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 27, 2020
guillaumeyan added a commit to guillaumeyan/bugspringtest that referenced this issue Oct 30, 2020
@sbrannen
Copy link
Member

sbrannen commented Nov 4, 2020

@wilkinsona, thanks for bringing this topic up again. We will investigate options for addressing this issue in Spring Framework 5.3.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

4 participants