• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

easymock beginner

 
zb cong
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • 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
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic