-2

I have an assertion that checks if a string is present in the currently selected text:

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.containsString;
assertThat(latest.getText(), containsString(targetString));

But I can't find the right way to write an assertion that checks if a string is NOT contained in the text.

I tried

assertThat(latest.getText(), not(containsString(targetString)));

but get an error

the method not(Matcher <String>) is undefined

What is the way to do this?

ruud
  • 743
  • 13
  • 22

2 Answers2

5

to me this is working and raises an error:

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;

class Main {
  public static void main(String[] args) {
    String test = "qwerty";
    String contained = "ert";
    assertThat(test, not(containsString(contained)));
  }
}
rakwaht
  • 3,666
  • 3
  • 28
  • 45
1

You could switch to AssertJ and then use

assertThat(latest.getText()).doesNotContain(targetString);
tomasulo
  • 1,252
  • 8
  • 9