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 Test for a method containing a call to another method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Test for a method containing a call to another method" Watch "Test for a method containing a call to another method" New topic
Author

Test for a method containing a call to another method

Sahil Reddy
Ranch Hand

Joined: Jan 24, 2011
Posts: 145
Hello

I have a method A() wherein i call method B() . I want to test whether method A() has a statement calling method B() or not. How can i achieve it in Junit 4 .

Thanks
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30894
    
158

I can think of two options:

1) If method B does something, assert whether it happened
2) Create a dummy subclass of the class you are trying to test and have it override method b. Then you can set a variable in your subclass and know if b was called.


[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
Gopi Chella
Ranch Hand

Joined: Apr 26, 2010
Posts: 53
Just have a boolean getter and setter methods in your JUnit class, and from MethodB just pass "true" to setter method , so if Method A(Or any Method) called Method B then you will come to know through the value returning by the getter method.


SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Test for a method containing a call to another method