java – How to test a method that changes letters in a word?


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;
        return charArray;

I just can’t figure out what to check here. So I started writing …

public class TestMyClass {

    public void test() {
        MyClass c = new MyClass();
        char[] word = {'h', 'e', 'l', 'o'};
        int first = 0;
        int last = word.length-1;



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:

  1. Method test on correct data (we check that the method correctly swaps array characters):

public void validInput_shouldSwap() {...}

  1. Method test for negative values ​​of indexes (index):

public void negativeIndex_shouldThrow() {...}

  1. 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.

Scroll to Top