I've injected HttpServletRequest into a bean. How do I unit test it? - spring

I have a bean in which I've injected an HttpServletRequest using the #Autowired annotation.
This injection works correctly when the application context is a web application Context. That's not the case for application contexts for JUnit tests with Spring.
How can I test this bean ? Maybe I can mock an http request, but then how to inject this mock in the bean ?
This is on Spring 3.0 and Junit 4.4

Create a bean of type MockHttpServletRequest and add it to your test context. This should then be autowired into your target bean.

Related

CDI #ApplicationScoped bean in Spring

I have a CDI bean that is annotated with #ApplicationScoped. Is there a way to tell Spring to pick it up during component scan, as if it were annotated with #Component? Spring does understand the #Inject annotation so why not #ApplicationScoped?
The idea is that it would be handy to use CDI beans in Spring (at least if they only use plain dependency injection without the fancy CDI stuff like interceptors, decorators...)
It is not entirely clear how is your code structured, if possible just annotate it with #Component as well. A component bean in Spring has similar properties as an application scoped bean. The default scope of a bean in Spring is singleton and it can be proxied, similar to what #ApplicationScoped would offer.
Spring does understand the #Inject annotation so why not #ApplicationScoped?
Spring offers support for JSR 330 annotation, #ApplicationScoped is nor part of those.

How to resove java.lang.IllegalArgumentException: Property 'dataSource' in Junit for Springboot Rest

The error is thrown while trying to unit test the Service class that contains an autowired Dao object which internally uses an Autowired JdbcTemplate in Spring boot Rest service using Junit and Mockito
in you `SpringBootTest you should pass config classes or properties files where all configs are.
Probably dataSource is missing
#SpringBootTest(classes = YouAppRunnerOrConfig.class, properties = "...")

Spring boot jersey - prevent startup instantiation of controller

I am using spring boot with web and jersey (spring-boot-jersey-starter). I have a Jersey endpoint that needs to inject a request scope bean. However, at startup of the application I am getting a no bean found error.
#Component
#Path("blah")
#RequestScoped
public class JerseyController{
#Inject
private MyEntity entity;
}
#Component
public class JerseyConfiguration extends ResourceConfig{
public JeyseyConfiguration(){
register(JeyseyController.class);
registere(MyEntityProvider.class);
}
}
Is there a way, in a spring-boot web app, to prevent Spring from attempting to instantiate and inject my JerseyController until an HTTP request is received so that the injected dependency can be provided by my Jersey provider?
#Component is not required on Jersey resources. Having it will cause Spring to instantiate it (with default Singleton scope). I don't think Spring doesn't respect the #RequestScoped. This is a Jersey annotation. If you want to use the #Component, I think the Spring #Scope("request") might do the trick though.
You can also remove the #RequestScoped. This is the default scope for Jersey resources.
The only time I have ever found a need to use #Component on Jersey resources, is if I need to use the Spring #Value (maybe AOP also, but I don't do much AOP). Other than that, the Jersey-Spring integration already supports the most common used feature of Spring which is DI. And if you really want to make the Jersey resource a singleton, Jersey supports the #Singleton annotation.

Using session-scoped object in singleton in Spring

I have a problem with one of my beans. I have a bean, which has a singleton scope and second bean which has session scope.
I'm using java based config in my spring app, I added proxy mode to my session scope bean, but it throws exception when I'm tryign to use that (session scoped) bean:
Method threw 'org.springframework.beans.factory.BeanCreationException' exception. Cannot evaluate my-bean-full-name$$EnhanceBySpringCGLIB#ID.toString()
From what I have read here http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-scopes-other-injection-proxies
I thought that only thing which I have to do is to add
#Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
to my bean class (which also has #Component annotation and it is scanned by Spring)
The thing which I was missing was this line in my web app initializer:
servletContext.addListener(new RequestContextListener());
After that, bean has been injected

spring access beans during context initialisation

I want to access the Spring Applciation Context , when the entityManagerFactory is getting initialised.
We can use ApplicationContextAware , but the entityManagerFactory gets instantiated before our ApplicationContextAware bean is initialised.
Share if anybody has done something like this .
You could subclass entitymanagerfactory and give it a constructor with ApplicationContext, and wire the context into the constructor.
Note that tampering with the applicationcontext while it's still being initialized is strongly discouraged.
Configure the bean to use ApplicationContextAware and then set the SessionFactory bean's depends-on attribute to the context aware bean. This should cause the bean to be created prior to the sessionFactory.

Resources