Junit: overriding original class private methods

R

Robert M. Gary

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
 
J

Jim Korman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top