Junit with spring Autowired

1

I have a big problem that in my GenericService I use an @Autowired in an HttpServletRequest (Even though I do not know the utility, therefore, I do not know the architecture so well), and I needed to use unit test for test battery in my services, however, when I'm injecting the bean into my test, spring can not inject this HttpServletRequest as it did before in the container.

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'br.com.field.service.TalhaoService':
Injection of autowired dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not autowire
field: private javax.servlet.http.HttpServletRequest
br.com.visioncore.service.GenericServiceImpl.httpRequest; nested exception
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [javax.servlet.http.HttpServletRequest] found for
dependency: expected at least 1 bean which qualifies as autowire candidate
for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

Does anyone know how to test this? I have already been able to get my context settings inside the Test and I only need this now.

    
asked by anonymous 26.08.2014 / 14:02

1 answer

1

There is no real error

This behavior is completely natural and expected.

Reason: An HTTP request (or class that encapsulates such a request) will only actually exist in the web environment provided by a container or Application Server.

Unit testing and web layers

Unfortunately it is not very easy or productive to do unit tests of implementations that specifically deal with requests or the system view, in short, they are web components and it is tricky to deal with them from that environment.

The ideal would be to remove all logic from the application and put it in business components and then focus on the unit tests on those layers.

Integration test

Controller layers and views can be tested on a slightly higher-level test type, for example, in an integration test.

You can create a unit test that boots the system into a "built-in" container like Jetty or Tomcat. Then use a client API to make HTTP requests and simulate a user. Or you can even use Selenium / WebDriver to really open the browser and simulate the use of the system. Spring Boot for web , which already adds Tomcat dependencies and allows you to start the system with a line of code, can help if you can adapt your project.

Use Mocks

Another more specific possibility for your case is to create a mock of the missing object. Create a class that implements HttpServletRequest and returns fixed values for your test. Then set it to a Spring XML separately and include the XML only in the test class that needs it.

The problem with this is that mocks can fool you, that is, the test can work (since you put dummy values) and in a real situation the browser or the HTTP client ends up sending values the HTTP request.

My recommendation is to use mocks only where there is some important logic that is worth testing and can really be validated.

    
26.08.2014 / 15:14