2016-10-12 15 views
5

Co jest przeciwieństwem zawiera?naprzeciwko zawiera w hamcrest

List<String> list = Arrays.asList("b", "a", "c"); 
    // should fail, because "d" is not in the list 

    expectedInList = new String[]{"a","b", "c", "d"}; 
    Assert.assertThat(list, Matchers.contains(expectedInList)); 


    // should fail, because a IS in the list 
    shouldNotBeInList = Arrays.asList("a","e", "f", "d"); 
    Assert.assertThat(list, _does_not_contains_any_of_(shouldNotBeInList))); 

co powinno być _does_not_contains_any_of_?

+0

można po prostu neguje 'metody contains'? tj. '! zawiera (shouldNotBeInList)' Nie zrobiłem wiele z tego, więc nie jestem pewien, czy to zadziała, ale warto spróbować. –

+0

@ Michael Pickett: nie (zawiera wszystkie elementy) = zawiera nie wszystkie elementy – pihentagy

+0

Używasz Hamcrest? Jeśli tak, to składnia powinna być bardziej podobna! AnyOf (...). Myślę, że patrzę na API, ponieważ nigdy nie używałem Hamcrestu. –

Odpowiedz

4

Można łączyć trzy wbudowane dopasowujących się w następujący sposób:

import static org.hamcrest.Matchers.everyItem; 
import static org.hamcrest.Matchers.isIn; 
import static org.hamcrest.Matchers.not; 

@Test 
public void hamcrestTest() throws Exception { 
    List<String> list = Arrays.asList("b", "a", "c"); 
    List<String> shouldNotBeInList = Arrays.asList("a", "e", "f", "d"); 
    Assert.assertThat(list, everyItem(not(isIn(shouldNotBeInList)))); 
} 

Wykonanie tego testu otrzymasz:

Oczekiwany: każda pozycja nie jest jednym z { "a", "e", "F", "D"}
ale: pozycja była "a"

+0

Dzięki za odpowiedź. Teraz mam podobne pytanie dotyczące ścisłości matchera: http://stackoverflow.com/questions/40015862/strict-matching-in-hamcrest – pihentagy

0

Od JavaDoc widzę niewygodny sposób na zrobienie tego. Prawdopodobnie jest lepszy sposób! Te testy czy lista nie zawiera a i nie zawierają b i ...

List<Matcher> individual_matchers = new ArrayList<Matcher>(); 
for(String s : shouldNotBeInList) { 
    individual_matchers.add(Matchers.not(Matchers.contains(s)); // might need to use Matchers.contains({s}) - not sure 
} 
Matcher none_we_do_not_want = Matchers.allOf(individual_matchers); 
Assert.assertThat(list, none_we_do_not_want); 

(nie testowane, prawdopodobnie buggy:/nadzieję, że to pomaga)

0

jako obejście można wykorzystać następujące:

listy - shouldNotBeInList powinien być równy dla samej listy (trzeba konwertować, aby ustawić)

Set<String> strings = new HashSet<>(list); 
    strings.removeAll(shouldNotBeInList); 

    Set<String> asSet = new HashSet<>(list); 
    Assert.assertTrue(strings.equals(asSet)); 

Ale nie powinno być lepiej w Mam nadzieję, że.

1

Spróbuj tej metody:

public <T> Matcher<Iterable<? super T>> doesNotContainAnyOf(T... elements) 
{ 
    Matcher<Iterable<? super T>> matcher = null; 
    for(T e : elements) 
    { 
     matcher = matcher == null ? 
      Matchers.not(Matchers.hasItem(e)) : 
      Matchers.allOf(matcher, Matchers.not(Matchers.hasItem(e))); 
    } 
    return matcher; 
} 

W tym przypadku testu:

List<String> list = Arrays.asList("a", "b", "c"); 
// True 
MatcherAssert.assertThat(list, doesNotContainAnyOf("z","e", "f", "d")); 
// False 
MatcherAssert.assertThat(list, doesNotContainAnyOf("a","e", "f", "d")); 
0

miałem mieć takie same kwestia. Moje rozwiązanie było po prostu odwróconą logiką dla tego meczu.

wyniku można zobaczyć fragment kodu:

this.mockMvc.perform(get("/posts?page=0&size=1") 
       .with(httpBasic(magelan.getUserName(), magelan.getPassword())) 
       .accept(MediaType.parseMediaType("text/html;charset=UTF-8"))) 
       .andExpect(status().isOk()) 
       .andExpect(content().contentType("text/html;charset=UTF-8")) 
       .andExpect(content().string(allOf(
         containsString("First post") 
       ))) 
       .andExpect(content().string(allOf(
         not(containsString("Second post")) 
       ))); 
Powiązane problemy