• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the refactoring I suggested by adding a parameter? That allows the mock to be injected.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Talk to the owner. Why are you responsible for testing code you aren't allowed to touch?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic