Arthur
Arthur

Reputation: 25

What are best practices for testing methods which call similar method of same class with different parameter types?

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

Answers (1)

GhostCat
GhostCat

Reputation: 140613

A reasonable approach:

  • first focus your testing on that method that really does "do the work" (in your example, that would be the first version). You want to understand the public contract of that method and test that intensively
  • then, afterwards look at those other methods that simply "wrap" around that already well-tested method.

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:

  • when a method is doing "real business" - test that "real business"
  • when the method is only about plumbing/gluing things together - then focus on that aspect - instead of re-testing the thing that gets "wrapped around"

Upvotes: 3

Related Questions