Is there a nicer way to write in jUnit
String x = \"foo bar\";
Assert.assertTrue(x.contains(\"foo\"));
It's too late, but just to update I got it done with below syntax
import org.hamcrest.core.StringContains;
import org.junit.Assert;
Assert.assertThat("this contains test", StringContains.containsString("test"));
You can use assertj-fluent assertions. It has lot of capabilities to write assertions in more human readable - user friendly manner.
In your case, it would be
String x = "foo bar";
assertThat(x).contains("foo");
It is not only for the strings, it can be used to assert lists, collections etc.. in a friendlier way
Use the new assertThat
syntax together with Hamcrest.
It is available starting with JUnit 4.4.
assertj variant
import org.assertj.core.api.Assertions;
Assertions.assertThat(actualStr).contains(subStr);