This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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?
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.
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.
Joined: Jul 08, 2010
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!