Junit: overriding original class private methods  
Author Message
Robert M. Gary





PostPosted: 2006-12-29 9:23:00 Top

java-programmer, Junit: overriding original class private methods I have a class that when run will call a private sendEfvent() method
that sends data to an external process. I would like to override that
behavior in testing and just report what is set to avoid the
complications, etc of having to use external processes during unit
testing.

I don't think I can just use an assertTrue() because I also want to
know how many times this private method is called (i.e. is it sending
the correct number of copies out)?

Is there a way I can cause Junit to stop the JRE from calling the true
sendEvent() method and instead call something in my test class??

-Robert

 
Jim Korman





PostPosted: 2006-12-29 9:37:00 Top

java-programmer >> Junit: overriding original class private methods On 28 Dec 2006 17:23:22 -0800, "Robert M. Gary" <email***@***.com>
wrote:

>I have a class that when run will call a private sendEfvent() method
>that sends data to an external process. I would like to override that
>behavior in testing and just report what is set to avoid the
>complications, etc of having to use external processes during unit
>testing.
>
>I don't think I can just use an assertTrue() because I also want to
>know how many times this private method is called (i.e. is it sending
>the correct number of copies out)?
>
>Is there a way I can cause Junit to stop the JRE from calling the true
>sendEvent() method and instead call something in my test class??
>
>-Robert

Consider using an Interface that exposes the send event behavior.
Then your sendEfvent() method calls this strategy's send method.

During testing you can set the strategy to a mock implementation that
counts data, or better yet, exposes the message so that you can then
unit test the message contents.

Jim