File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes Mock a new operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Mock a new operator" Watch "Mock a new operator" New topic
Author

Mock a new operator

dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
There is a lot of refractoring that needs to be done on the current spring application which I am working on for unittesting.
But we are not suppose to go with refractoring...There is a strict no no from clients.

I would like to create a mock for an " instance created in a class using a new operator". because they didn't go with the
dependency injection.(no setters/constructors).
Below is the code


My Mock Test case: I am creating Mock on the "metastoreDAOManager" as below




As there is no setter/Constructor injection done for "metastoreDAOManager" in searchcontroller.class I am not able to perform the mock on ""MetastoreDAOManager"".

Can you please let me know how to achive mock on the "new operator" created in a class..


Note:
We are not allowed for refractoring.



Mythoughts:
Create a proxy for the below code when getUserPreferenceFromMetastore(request) is called.using spring AOP.Is this the right way:
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

Dileep,
Since it is an instance variable, you can use reflection. The Spring testing jar even provides a method ReflectionTestUtils to make it easy/one line to inject an instance variable into a class.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Jeanne,
Thanks for the information!!

But the problem is, even if we create an instance using reflection in the test case this is not going to help!!
Because once the "showForm(MockHttpServletRequest request,MockHttpServletResponse response, BindException arg2)"
method in the SearchController class is called , the below code
MetastoreDAOManager metastoreDAOManager = new MetastoreDAOManager();
will create a new instance.

There by the metastoreDAOManager object created in the testcase will evade.


Please correct me if my understanding is wrong...
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Tried with jmockit but of no luck.




Came to a conclusion that without refractoring code its tough to go a head.

Will go with complete flow testing using Junit.

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

If you are using spring then you shouldn't really be using the new operator at all, certainly not on any object which is likely be be reused or have other things injected into it (usually you would inject your datasource into your dao).
Your DAO should probably be a class variable as well... since it should be stateless and thus you will only need one instance to hadle all requests...

Here is a very simple snippet to illustrate what I mean:


This makes testing simpler as you can use the test helper classes in Spring to inject a mock metastoreDAOManager and do as you wish.

Hope this makes sense.

Sean


I love this place!
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Thanks for the response!!

As told earlier in my earlier post, the clients doesn't want the code to be refractored.
Thats where the challenge has come when doing the unittesting, as they didn't use the framework properly.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Ah, my apologies, I must have missed that.
In that case you may want to check this out powermock. The exaple given shows you how to mock constructors and the example is pretty much what you need.
This isn't so much a mocking framework in itself but it adds extensions and integrates with most of the popular mocking frameworks (I used Mockito).

Something to have a look at anyway!

hope that helps and good luck!

Sean
Rogerio Liesenfeld
Greenhorn

Joined: Jan 24, 2011
Posts: 3
JMockit can do it easily. The following test will work:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock a new operator
 
Similar Threads
Testing web application using Spring Mock
Getting a headage..... Cannot find class [...] for bean with name '...' defined in ServletContext re
"Failed to load ApplicationContext" exception when trying to run JUnit Spring test
display exception on formbackingobject
java.lang.IllegalStateException: getOutputStream() error