Problem
I use Test-Driven Development and find it difficult for my tests to define my code well enough. A simple example of my problem is as follows.
I have MyObjectone from which I want to call either methodA(), or methodB(), belonging OtherObject, depending on which argument MyObjectgets on its own callMethod(int).
Expected code (and desired functionality)
This is essentially what I want to make the code, but first I want to execute it.
public class MyObject {
private final OtherObject otherObject;
public MyObject(OtherObject otherObject) {
this.otherObject = otherObject;
}
public void callMethod(int i) {
switch (i) {
case 0:
otherObject.methodA();
break;
case 1:
otherObject.methodB();
break;
}
}
}
Writing this test first
To do this, I'm starting with the writing test - check that the call callMethod(0)is invoked methodA(). I use JUnit and Mockito.
public class MyObjectTest {
private final OtherObject mockOtherObject = mock(OtherObject.class);
private final MyObject myObject = new MyObject(mockOtherObject);
@Test
public void callsMethodA_WhenArgumentIs0() {
myObject.callMethod(0);
verify(mockOtherObject).methodA();
}
}
/, , , MyObject :
public void callMethod(int i) {
otherObject.methodA();
}
- callMethod(1)
@Test
public void callsMethodB_WhenArgumentIs1() {
myObject.callMethod(1);
verify(mockOtherObject).methodB();
}
:
public void callMethod(int i) {
otherObject.methodA();
otherObject.methodB();
}
, , . , ? , . , , , .
2 , , , , , , . , , .
, 3 - 3 - 6, , 2 ? ( , .)
, .
if switch, .