aspose file tools*
The moose likes Testing and the fly likes Spring-Test-MVC & Mockito Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Spring-Test-MVC & Mockito" Watch "Spring-Test-MVC & Mockito" New topic
Author

Spring-Test-MVC & Mockito

Darren Carter
Ranch Hand

Joined: Jul 08, 2010
Posts: 30
Hi Everyone,

Ive been having a play the spring-test-mvc library and very impressed with it. I have one small issue in which I was hoping someone could help?

Im using mockito and am having problems with the MockHttpServletRequest.

My unit test



My issue is that mockito never matches the mocked request in the controller and returns null.

I would like it to return the productApplicationContext mocked object.

In my controller I have



Can anyone kindly give some advise? It appears the request in my unit test is not the request that the controllers receives.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

I guess first off when I see



in code that is clearly Spring 3.x I wonder why that is necessary at all? I think you are inadvertently trying to test the framework. There is no need to do this, the framework is tested. Ideally you will test a controller method like any other and test just your small bit of logic.

Your controller should be annotated with @Controller and you should be able to just @Autowire your dependencies in. This also has the advantage of decoupling your controllers from the Spring framework and making things much easier to test. If there is a legitimate need to get the context have a look at using WebApplicationContextUtils, but once again I would guess that in almost all cases this is not necessary, rather you should be having Spring inject only the dependencies you need from the context.

Rather than trying to test this in this manner, I would post in the Spring forum and maybe get some suggestions on how you can refactor your Controller logic to be more test friendly.

[How To Ask Questions][Read before you PM me]
Darren Carter
Ranch Hand

Joined: Jul 08, 2010
Posts: 30
Hi Bill,

Thank you for the reply - much appreciated.
The line of code is not actually spring and is poorly named within an application that I am currently refactoring. Appologies for confusion.

The above snippet is in fact is pulling attributes from the HttpServletRequest and and populating the ProductApplicationContext object - I will refactor this so that the required data is parsed in via Json from the request, and then use @RequestBody to populate the ProductApplicationContext object.

The controller im testing is a Rest controller that contains all the required annotations and I am injecting mock objects into my @Autowired attribites. This all works fine.

My main issue is that mockito ia unable to capture the pattern below as the request in my unit test (MockHttpServletRequest) is not the same object as the MockHttpServletRequest sent to the controller. So I dont get a object returned, and just a Null pointer



This is because creates a new MockHttpServletRequest object each time so i am unable to supply mockito with the correct request object proir to this execution. So somehow I need to get hold of and control the MockHttpServletRequest object that is sent to the controller. Ive had a look and it appears that I may need to extend one of the requestBuilder classes, but thought Id ask if there is any other way to do this?

Thanks once again
Darren.


Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

I would definitely take a look at refactoring that. There are a number of things you can do that would be better than what you have got going there.

To your original question. Unit tests should test small units. Lets assume your requestHelper is some sort of a service, this should be tested separately not as part of the controller. I will give you an example, I use java config because it feels more natural to me but you can easily do this with XML as well. We will use a controller a service and two test classes, one to test the controller the other to test the service. I have structured it similar to what I see in your code snippets.

TestController.java


Service interface and implementation


The object we are returning as JSON this would be synonymous with your Response object



Spring unit test best practices say to have a separate test config for each test class, so we will define those next.





Now the following test case tests only the Service not the controller



This test case tests only the controller not the service. The service is mocked.



I ran this just to make sure there are no mistakes and I got a green bar. Hopefully this helps you, but I would still strongly consider refactoring what you have.
Darren Carter
Ranch Hand

Joined: Jul 08, 2010
Posts: 30
Hi Bill,

This is a fantastic post and Im going to give this a try. I totally agree that java config seems more natural and this looks like a much better to way to test using the standalone setup - I dont need to load/configure the test servlet context.

Thank you very much for spending the time on this!

Best Regards
Darren.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

I thought I would post some of the dependencies since it may not be obvious to future readers.



Remember to extract your versions to properties this example was just for illustration.

One last things you will need some static imports in the controller test to write it the way I did. Alternatively you can access them statically through the MockMvcResultMatchers class.

Here are the static imports I had:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring-Test-MVC & Mockito
 
Similar Threads
Spring Basic Question.. Unit Testing Made simple in Spring ? How?
Testing Spring Controller
UnitTest in Spring
Mocking Method call within a Method?
Mockito "when" problem