devoured elysium
devoured elysium

Reputation: 105067

How to unit-test 2 methods in the same class where the first method calls the second method?

Let's say I have a class MyClass with 2 public methods, methodA() and methodB(), both returning the same type of object.

methodA() will do some calculations and then call methodB.

How should I test this class? Will I have to repeat all the tests I did for methodA() on methodB()?

How to approach this?

Thanks

Upvotes: 2

Views: 310

Answers (3)

Chris
Chris

Reputation: 21

It depends on the code being tested and how thorough you want to be. If methodA()'s outcome is effected by methodB() you could test just methodA(), however as duffymo says it's best to test both, and as robert says its best to code the test for methodA() as if you didn't know it was implemented through methodB() and the same for methodB(). Since both methodA() and methodB() are public, each should be tested separately. But it's best if the test for methodA() assures methodA() did its job and the test for methodB() assures methodB() did its job, so the tests shouldn't really be the same.

Upvotes: 1

robert
robert

Reputation: 34398

You should test methodA() as if you don't know how it's implemented. Then, if you change it to use methodC() instead of methodB() in the future, you still have coverage.

Upvotes: 2

duffymo
duffymo

Reputation: 308763

Test both independently, because that's how clients will see them.

Upvotes: 2

Related Questions