Reputation: 25
What is the best practice to test method, which calls similar method of same class with different parameter type?
Should an instance be mocked or no? Etc.
Have you examples or solution for current one?
I am in interested in best practice for cases, when tested method is more, than 1 row and copying of tested code to test class is problematic.
class A {
Map z = //...
public void put(String x, Y y) {
z.put(x, y);
}
public void put(String x, String y) {
put(x, Y.parse(y));
}
}
Upvotes: 1
Views: 164
Reputation: 140613
A reasonable approach:
For the later methods you only want to test what this method is doing. In your example that would mean: you make sure that the incoming string goes into Y.parse().
I would consider it bad practice to test your second method with the same scrutiny that you should apply to the first one.
Your tests should focus on that "work" that a method is responsible for. In your example, the first method is responsible for some "real business stuff"; whereas the second method is (on the surface) only responsible for converting an argument and invoking another method.
Meaning: when you have 5 tests for the first method put(String, Y)
(that test all different paths within that method) - you might need only 1 test for method put(String, String)
(which simply checks that some expected path in put(String, Y)
is taken because of the things that put(String, String)
is doing).
TL;DR:
Upvotes: 3