Search code examples
javajunit

Name ascending @FixMethodOrder not working for Junit tests


While testing my own ArrayList implementation, I set up an instance of MyArrayList class before tests and in order to check whether my logic works how it should in implemented methods I used @FixMethodOrder(MethodSorters.NAME_ASCENDING). However, once I run my tests, the order to of tests that were executed is not lexicographical.

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.FixMethodOrder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.runners.MethodSorters;

import java.util.List;
@TestInstance(Lifecycle.PER_CLASS)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public abstract class ListTest {

  protected abstract List<Integer> provideList();

  private List<Integer> list;

  @BeforeAll
  public void setUp() {
    list = provideList();
    for (int i = 0; i < 100; i++) {
      list.add(i);
    }
  }

  @Test
  public void test1ShouldReturnActualSizeWhenListIsNotEmpty() {

    //when
    int actual = list.size();

    //then
    assertEquals(100, actual);
  }


  @Test
  public void test2ShouldReturnFalseWhenListIsNotEmpty() {

    //when
    boolean actual = list.isEmpty();

    //then
    assertEquals(false, actual);
  }

  @Test
  public void test3ShouldReturnTrueWhenListContainsElement() {

    //when
    boolean actual = list.contains(4);

    //then
    assertEquals(true, actual);
  }

  @Test
  public void test4ShouldReturnFalseWhenListDoesNotContainElement() {

    //when
    boolean actual = list.contains(200);

    //then
    assertEquals(false, actual);
  }

  @Test
  public void test5ShouldReturnGivenListAsArray() {

    //when
    Object[] actual = list.toArray();
    Object[] expected = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
        44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
        67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
        90, 91, 92, 93, 94, 95, 96, 97, 98, 99};

    //then
    assertArrayEquals(expected, actual);
  }
}

Here is the order of my tests that were executed. It always remains the same, no matter how I change the name of my test methods:

Test Results

Where did i make a mistake? I have also tried to use the annotation not on this abstract class but on the class that inherits from it, but it didn't help either.


Solution

  • You are combining Junit4 and Junit5. Currently Junit5 does not support order of methods: https://github.com/junit-team/junit5/issues/13 .

    You will need to just use Junit4.