In an app I'm working in there's a service class(among others) which is used just to call methods on other classes. Recently I've had to work in that area and add another method that does what all the other methods do, call other methods from other classes. Arguably, this whole class shouldn't exist, but that's another topic.
The unit tests that exist for other methods in that class seem to be doing a lot busy work. Going as far as checking the values returned from a mock.
In my eyes this isn't worth writing a unit test for other than confirming that X
method is called from Y
class. Also known as verify
in Java/C#, spy
in PHP , called
in Python etc.
Here's some pseudo code to get an idea of what I mean
public class SomeClass{
public void methodA() { ... }
}
public class SomeOtherClass {
public String methodB() { ... }
}
public class Service {
public Service(SomeClass someClass, SomeOtherClass someOtherClass) {
...
}
public void doWork() {
this.someClass.methodA()
this.someOtherClass.methodB()
}
}
A unit test would look like this
public class UnitTest {
public void testStuff() {
SomeClass someClass= mock(SomeClass.class);
Service service = ...;
verify(someClass).methodA();
}
}