| 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
|
|
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
|
 |
 |
|
|
subject: Test for a method containing a call to another method
|
|
|