aspose file tools*
The moose likes Testing and the fly likes Mocking local object created from the spring application context by using mockito framewrk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Mocking local object created from the spring application context by using mockito framewrk" Watch "Mocking local object created from the spring application context by using mockito framewrk" New topic
Author

Mocking local object created from the spring application context by using mockito framewrk

S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Hi All,
Am trying to mock local object by using Mockito framework ,that is being created from the spring application context .But every time when am running the application by TestNG its failed to replace the original object by the mocked object.
Here is the original class's code spinets:

public void executeMyJob(){

ApplicationContext springContext = ApplicationContextUtil.getApplicationContext();
MusicEAO music= (MusicEAO) springContext.getBean("MusicEAO"); List<Brand> dataList =music.getAll();
......
......
}

I want to mock the MusicEAO and when the getAll() method being called it will call the mock object.

Below is my test class code snippets:

@Mock
MusicEAO musicEAO ;


when(musicEAO.findAll()).thenReturn(myDefinedList);

Can anybody help me out this ?

Thanks and warm regards,
Satya
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150


This part of the code is a problem. You aren't injecting the mock anywhere. You are still referring to the Spring context.

In general, I wonder why you are using getBean() directly rather than having it injected.

A more useful suggestion is to refactor your code to

That way you can mock out the ApplicationContext as well and pass the mock in.


[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
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Jeanne Boyarsky wrote:
This part of the code is a problem. You aren't injecting the mock anywhere. You are still referring to the Spring context.

In general, I wonder why you are using getBean() directly rather than having it injected.



Jeanne ,,,you are correct , this is the original class.
In my test class am mocking the MusicEAO class by the mockito framework by using the @Mock annotation.But the original object is not getting mocked....

Can any body help me out ?

Thanks,
Satya

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Did you try the refactoring I suggested by adding a parameter? That allows the mock to be injected.
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Hi Jeanne,
I am not the owner of the code ... So am not able to refactor .Any other option do you have ?

Thanks ,
Satya
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Talk to the owner. Why are you responsible for testing code you aren't allowed to touch?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mocking local object created from the spring application context by using mockito framewrk