Kolekcja w jUnit?

Czy istnieje jUnit równoległy do zbioru NUnit ?

Author: R. Martinho Fernandes, 2009-07-06

4 answers

Używając JUnit 4.4 możesz użyć assertThat() razem z Hamcrest (nie martw się, jest dostarczany z JUnit, nie ma potrzeby dodatkowego .jar) do tworzenia złożonych samoopisujących twierdzeń, w tym tych, które działają na kolekcjach:

import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.*;
import static org.hamcrest.CoreMatchers.*;

List<String> l = Arrays.asList("foo", "bar");
assertThat(l, hasItems("foo", "bar"));
assertThat(l, not(hasItem((String) null)));
assertThat(l, not(hasItems("bar", "quux")));
// check if two objects are equal with assertThat()

// the following three lines of code check the same thing.
// the first one is the "traditional" approach,
// the second one is the succinct version and the third one the verbose one 
assertEquals(l, Arrays.asList("foo", "bar")));
assertThat(l, is(Arrays.asList("foo", "bar")));
assertThat(l, is(equalTo(Arrays.asList("foo", "bar"))));

Dzięki temu podejściu automatycznie uzyskasz dobry opis twierdzenia, gdy się nie powiedzie.

 111
Author: Joachim Sauer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-13 07:28:45

Nie bezpośrednio, nie. Sugeruję użycie Hamcrest , który zapewnia bogaty zestaw reguł dopasowywania, który dobrze integruje się z jUnit (i innymi frameworkami testowymi)

 4
Author: skaffman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-13 07:27:57

Przyjrzyj się twierdzeniom FEST. IMHO są wygodniejsze w użyciu niż Hamcrest (i równie wydajne, rozszerzalne itp.) i mają lepszą obsługę IDE dzięki płynnemu interfejsowi. Zobacz https://github.com/alexruiz/fest-assert-2.x/wiki/Using-fest-assertions

 2
Author: Tomek Kaczanowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-19 18:50:45

Rozwiązanie Joachima Sauera jest ładne, ale nie działa, jeśli masz już szereg oczekiwań, które chcesz zweryfikować w swoim wyniku. Może się to pojawić, gdy masz już wygenerowane lub stałe oczekiwania w testach, z którymi chcesz porównać wynik, lub być może masz wiele oczekiwań, które oczekujesz połączenia w wyniku. Więc zamiast używać matcherów możesz po prostu użyć List::containsAll i assertTrue na przykład:

@Test
public void testMerge() {
    final List<String> expected1 = ImmutableList.of("a", "b", "c");
    final List<String> expected2 = ImmutableList.of("x", "y", "z");
    final List<String> result = someMethodToTest(); 

    assertThat(result, hasItems(expected1)); // COMPILE ERROR; DOES NOT WORK
    assertThat(result, hasItems(expected2)); // COMPILE ERROR; DOES NOT WORK

    assertTrue(result.containsAll(expected1));  // works~ but has less fancy
    assertTrue(result.containsAll(expected2));  // works~ but has less fancy
}
 1
Author: gavs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-18 16:25:12