Opposite contains in hamcrest

What does the opposite contain?

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))); 

What should be _does_not_contains_any_of_ ?

+5
source share
5 answers

You can combine the three built-in couplers as follows:

 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)))); } 

Performing this test will give you:

Expected: each item is not one of {"a", "e", "f", "d"}
but: element was "a"

+5
source

Try this method:

 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; } 

In this case:

 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")); 
+1
source

From JavaDoc, I see a clumsy way to do this. There is probably a better way! This checks if the list does not contain a and does not contain b and ...

 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); 

(not tested, possibly buggy: / hope this helps)

0
source

As a workaround, you can use the following:

list - shouldNotBeInList should equal the list itself (needs to be converted to a set)

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

But hopefully there should be a better way.

0
source

I had the same problem. My solution was just the inverted logic for this match.

Below you can see the code snippet:

 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")) ))); 
0
source

Source: https://habr.com/ru/post/1258087/


All Articles