• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

easymock beginner

 
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello i am new to easymock, i have two classes,A and B, A.ma() method call B.mb(), i want to mock the B.mb(), as follows:




regarding to my understand, once the B.mb() is mocked, when i test A.ma(), the "B is executed." should not be printed out, is that correct? let us see my test code as follows:



unfortunately, the output as follows:

B is executed.
return from B


it seems the easymock dont works yet, who can help? thanks.
 
author & internet detective
Posts: 39341
755
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You instantiate B directly in A's method rather than using the mock. Mocks can only help if you can inject them - pass them as a parameter, use them as an instance variable.
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oop, disappointed, but if that B class is a singleton, i think my code maybe works, correct?
 
Jeanne Boyarsky
author & internet detective
Posts: 39341
755
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on how the code is written. If it was B.getInstance(), you'd still have an awkward injection scenario. Better to refactor the code to actually use dependency injection.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!