aspose 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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » Testing
Reply 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: 143
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 26183
    
  66

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: 52
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Test for a method containing a call to another method
 
Similar Threads
Method Overloading Question
Calling super class method
can't call another constructor from no-arg constructor
Explain ...
Exceptionhandling