Is it poorly designed and implemented code to have a method that internally calls other methods?
并不是的.但是我要说的是,在这种情况下,应该测试调用其他方法的方法,就像其他未经过单独测试的方法一样.
也就是说,它可以保护您免受公共方法在没有注意到的情况下停止调用其他方法的情况.
是的,它(有时)会产生大量的测试代码.我相信这就是重点:编写测试的痛苦是一个很好的线索,你可能想要考虑将这些子方法提取到一个单独的类中.
如果我可以忍受那些测试,那么我认为子方法还没有被提取出来.
What is the best practice and/or approach in writing a unit test for such a method (assuming it is itself a good idea) if one has chosen Mockito as their mocking framework?
我会做那样的事情:
public class Blah {
public int publicMethod() {
return innerMethod();
}
int innerMethod() {
return 0;
}
}
public class BlahTest {
@Test
public void blah() throws Exception {
Blah spy = spy(new Blah());
doReturn(1).when(spy).innerMethod();
assertThat(spy.publicMethod()).isEqualTo(1);
}
}