Question:
I started to delve into the unit test, practiced on all sorts of simple arithmetic operations, everything is clear. There is a specific method that changes the letters in a word:
private static char[] exchangeCharInWord(char[] charArray, int first, int last ){
char tmp;
tmp=charArray[first];
charArray[first]=charArray[last];
charArray[last]=tmp;
return charArray;
}
I just can’t figure out what to check here. So I started writing …
public class TestMyClass {
@Test
public void test() {
MyClass c = new MyClass();
char[] word = {'h', 'e', 'l', 'o'};
int first = 0;
int last = word.length-1;
}
}
Answer:
It is necessary to highlight the possible input data of the method and the results of the method's work with this data. For example, these tests:
- Method test on correct data (we check that the method correctly swaps array characters):
public void validInput_shouldSwap() {...}
- Method test for negative values of indexes (index):
public void negativeIndex_shouldThrow() {...}
- Method test if indexes are out of bounds of the array:
public void outOfBoundIndexValues_shouldThrow() {...}
Depending on the implementation of method 2 and 3, tests can be combined into one.
Accordingly, inside each test, you check the results of the method with the expected ones.