Avi
Avi

Reputation: 39

How to verify number of call of private method in public method in Junit?

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.

Upvotes: 3

Views: 3155

Answers (1)

GhostCat
GhostCat

Reputation: 140523

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!

Upvotes: 11

Related Questions