File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes easymock beginner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "easymock beginner " Watch "easymock beginner " New topic
Author

easymock beginner

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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.


[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
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
oop, disappointed, but if that B class is a singleton, i think my code maybe works, correct?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: easymock beginner
 
Similar Threads
Printing a value returned by a method.
Unable to understand : No Last Call on a Mock available. _ Easy Mocks
what does this mean here?
Accessing an object without a reference
Doubt in equals method