opposite of contains in hamcrest
Asked Answered
A

5

22

What is the opposite of contains?

    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_?

Account answered 12/10, 2016 at 14:37 Comment(3)
Can you just negate the contains method? i.e. !contains(shouldNotBeInList) I haven't done much of this, so I'm not sure if it'll work, but its worth a try.Nook
@Michael Pickett: not(contains all elements) = contains not all elementsAccount
Are you using Hamcrest? If so, the syntax should be more like !anyOf(...). I think, just looking at the API as I've never used Hamcrest.Tanny
F
29

You can combine three built-in matchers in the following way:

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

Executing this test will give you:

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

Forfeiture answered 12/10, 2016 at 21:46 Comment(3)
Thanks for the answer. Now I have a related question about matcher strictness: #40016362Account
hmmm, and how to solve it for strings to check if a substring is not containedCostumer
after hasItems() method this is very counterintuitive. I was trying to use not(hasItems()) fro a while with no success. Thanks for your answer.Madid
M
4

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

With this test 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"));
Midgett answered 12/10, 2016 at 16:18 Comment(0)
S
2

From the JavaDoc, I can see a clunky way to do it. There is probably a better way! This tests whether the list doesn't contain a, and doesn't 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);

(haven't tested, probably buggy :/ hope it helps)

Shakiashaking answered 12/10, 2016 at 14:52 Comment(0)
C
1

I had a have the same issue. My solution was just inverted logic for this match.

Following you can see the snippet of code:

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"))
                )));
Chilopod answered 30/1, 2018 at 15:39 Comment(0)
A
0

As a workaround, one can use the following:

list - shouldNotBeInList should equal to the list itself (need to convert to set)

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

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

But there should be a better way, I hope.

Account answered 12/10, 2016 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.