Junit with spring Autowired

Asked

Viewed 803 times

1

I have a big problem that in my Genericservice I use an @Autowired on an Httpservletrequest (Even not knowing the utility, because, I do not know the architecture so well), and needed to use unit test for test battery in my services, however, when I inject the bean into my test spring cannot inject this Httpservletrequest as it could 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’ve managed to get my context settings inside Test and I just need it now.

1 answer

1


There’s no real mistake

This behavior is completely natural and expected.

Reason: an HTTP request (or the 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 perform unit testing of implementations that deal specifically with requests or view system, anyway, are web components and it is complicated to deal with them were of this environment.

The ideal would be to remove all application logic and put it into business components and then focus unit testing on those layers.

Integration test

The layers of controllers and views can be tested in a somewhat higher-level type of test, for example in an integration test.

You can create a unit test that initializes the system in one container "embedded" like Jetty or Tomcat. Then use an API client to make HTTP requests and simulate a user. Or you can use Selenium/Webdriver to actually open the browser and simulate system usage. The Spring Boot for web, which already adds Tomcat dependencies and allows system startup 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 missing object. Create a class that implements HttpServletRequest and return fixed values for your test. Then set it up in a separate Spring XML and only include XML in the test class you need it.

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

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

  • 1

    Thanks for the lesson, I get a clearer view now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.