0

I'm using JunitJupiter for unit testing. Now I'm at a point where I don't know how to check the order of a list.

Code:

@Test
@DisplayName("Should be in ascending order")
void sortAscending() {
    List<List<AccountlinevaluesEntity>> finalList = monthlySuSa;
    for (List<AccountlinevaluesEntity> list : finalList) {
        list.sort(Comparator.comparing(AccountlinevaluesEntity::getAccountNumber));
        assert(list, ...........);

    }
}

I'm pretty new to unit testing, i searched a lot but I didn't figure out how to do it.

So, is there any way to do this with jUnitJupiter or are there other ways to do this?

Hubi
  • 440
  • 1
  • 11
  • 25
  • 1
    This is a completely useless test. You're not testing anything, since the sorting is done inside your test method instead of a business method. So basically you're testing whether you wrote the test correctly (and whether `list.sort()` works, and we know it does). – Kayaman Dec 11 '19 at 12:51
  • 1
    Yeah, that's right. This method is called in another method. I assumed that I should check every single one of my methods when testing. – Hubi Dec 11 '19 at 13:05
  • 1
    So I test then whether the output of my business method was sorted correctly? or is it unnecessary to test how the sorting is, because we know that it will be correct? – Hubi Dec 11 '19 at 13:08
  • 1
    I would test the sorting if a flawed sort could result in loss of life or money. Otherwise it's just a formatting issue, and there are more important things to test. I suggest some theoretical tutorials about testing, to understand better a) what you need to test, b) what you **don't** need to test, and c) how to write code to be easily testable. Otherwise you'll be writing tests without really knowing if they're useful. – Kayaman Dec 11 '19 at 13:12
  • 1
    Okay, right. Thank´s :) – Hubi Dec 11 '19 at 13:16

1 Answers1

0

Try

assertThat(list, Matchers.contains("your", "items", "in", "order")

From JavaDoc

Creates a matcher for {@link Iterable}s that matches when a single pass over the examined {@link Iterable} yields a series of items, each logically equal to the corresponding item in the specified items. For a positive match, the examined iterable must be of the same length as the number of specified items.

Nicktar
  • 5,548
  • 1
  • 28
  • 43