views:

399

answers:

3

Is there a nicer way to write in jUnit

String x = "foo bar";
Assert.assertTrue(x.contains("foo"));
+1  A: 

How about Assert.assertTrue("foo bar".contains("foo"))? :-)

Adamski
Oh come on - It was a joke!
Adamski
I agree that SO needs a better humor outlet, but in the meantime, stick to humor in the comments. (I didn't downvote, BTW).
Yishai
Humour is not tolerable
oxbow_lakes
This wasn't downvoted due to SO intolerance of humor, this just wasn't funny :)
Tim Post
If you think that's bad check out some of the jokes here: http://stackoverflow.com/questions/234075/programmer-jokes-whats-your-best-one
Adamski
+3  A: 

Use the new assertThat syntax together with Hamcrest.

It is available starting with JUnit 4.4.

Robert Munteanu
+3  A: 

If you add in Hamcrest and JUnit4, you could do:

String x = "foo bar";
Assert.assertThat(x, Matchers.containsString("foo"));

With some static imports, it looks a lot better:

assertThat(x, containsString("foo"));
Yishai
A code sample always wins :)
ripper234
It doesn't compile :)
pjp