Home > Blockchain >  What are the steps to writing a JUnit Test for methods with arrays?
What are the steps to writing a JUnit Test for methods with arrays?

Time:11-10

I don't know what I am doing wrong with my JUnit test(below) because it keeps failing. Any help? The output would look something like this: "Result: 3!6!7!1!2!4"

@Test
public void getArrayString​() {
    int[] array = new int[] { 3, 6, 7, 1, 2, 4 };
    char symbol = '!';
    for (int n : array)
        System.out.print(symbol   n);
    assertEquals(3!6!7!1!2!4, array);

CodePudding user response:

You need to call the actual method in the unit test and assert the returned value. For example, if your method was in a class called SomeUtil:

  @Test
    void getArrayString() {
        int[] array = new int[] { 3, 6, 7, 1, 2, 4 };
        String returnedString = SomeUtil.getArrayString(array, '!');
        assertEquals("3!6!7!1!2!4", returnedString);
    }
}
  • Related