3
public void publicMethod() {
  for (i=1;i<10;i++)
    privateMethod();
}

private privateMethod() {
    something...
}

I need to write a JUnit testcase to verify the number of times the privateMethod() is called.

0

1 Answer 1

11

You shouldn't do that. You don't write unit tests to verify implementation details.

Your tests make sure that your public methods fulfill their contract. So you assert that things returned are as expected; or that subsequent calls to other methods give the desired output. Or you expect calls to throw specified exceptions. Or, third option: you injected mocked objects; which you later on verify that the mocks saw the method calls you specified upfront.

But writing test cases to specifically test private methods is really bad practice!

Sign up to request clarification or add additional context in comments.

5 Comments

should be the accepted answer. private methods are not what your class provides to the users of it, it the way it implements what it provides.
oh what a shame :)
Hope you'll get it, hopefully one day ill grow up and be as good as you are
Sure. Just keep going. And I see: I already helped you today with some upvotes; like stackoverflow.com/questions/39392907/… ...
Oh that was you ♥

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.