This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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


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: 32481
    
214

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.


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, 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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Test for a method containing a call to another method
 
It's not a secret anymore!