• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

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

 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 40676
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 40676
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 40676
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Talk to the owner. Why are you responsible for testing code you aren't allowed to touch?
 
get schwifty. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic