250

Is there a nicer way to write in jUnit

String x = "foo bar";
Assert.assertTrue(x.contains("foo"));
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 11
    IMO this is nice enough, suggested options are less readable – The Godfather Oct 26 '18 at 19:25
  • 10
    @TheGodfather less readable, but produce more meaningful assertion errors (ie, the accepted response will show the difference in strings, where as OPs solution will just show "False when expected True" on failure) – Mike Jun 12 '19 at 22:57
  • 1
    What makes an assert "nicer" is the error message when it fails. How readable it is in the code is secondary to that, because you don't have to look at the code until it fails, and the failure message is the first thing you see. – rjmunro Mar 12 '20 at 11:36
  • The question itself should be the accepted answer :D – d1Master May 05 '22 at 14:32
  • @rjmunro You can pass your own message as an [argument to assertTrue](https://junit.org/junit4/javadoc/4.13/org/junit/Assert.html#assertTrue(java.lang.String,%20boolean)), like `Assert.assertTrue("Should contain substring 'foo'", x.contains("foo"));` – rook218 Apr 13 '23 at 13:25

12 Answers12

374

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

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

With some static imports, it looks a lot better:

assertThat(x, containsString("foo"));

The static imports needed would be:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
masterxilo
  • 2,503
  • 1
  • 30
  • 35
Yishai
  • 90,445
  • 31
  • 189
  • 263
  • 8
    Be sure you're using `org.junit.Assert` versus `junit.framework.Assert`, as the latter doesn't have the Hamcrest Matcher `assertThat()` – Visionary Software Solutions Aug 08 '12 at 17:09
  • 16
    I think when running JUnit 4.10, the class to use is org.junit.matchers.JUnitMatchers, e.g.: assertThat("something", JUnitMatchers.containsString("some")); – Ewen Cartwright Feb 21 '13 at 13:04
  • 2
    The failure message for a failing `assertThat` is way more helpful then an `assertTrue` – Rylander Apr 01 '13 at 15:04
  • 3
    static imports needed are `import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.containsString;` - just to save someone from trouble – eis Aug 07 '13 at 16:03
  • 5
    ... and `org.hamcrest.Matchers.containsString;` in the latest api, in the `hamcrest-library` dependency. – eis Nov 26 '13 at 14:25
  • 1
    I faced 'cannot resolve method' issue too, but thanks to eis' comment above made the following changes. `import org.hamcrest.Matchers; import static org.hamcrest.MatcherAssert.assertThat; assertThat(, Matchers.containsString(""));` – Divya Jose Feb 27 '17 at 17:28
  • `org.hamcrest.CoreMatchers` is not available but hamcrest-core.jar (1.3) is in the classpath. What can I do? – Markus L Jun 26 '17 at 11:12
  • @MarkusL, you can examine the JAR to find the correct package. I believe that it has changed since 2009. – Yishai Jun 26 '17 at 11:17
  • For hamcrest-core 1.3 use this: `org.hamcrest.core.StringContains.containsString( String )` – Markus L Jun 26 '17 at 11:21
22

use fest assert 2.0 whenever possible EDIT: assertj may have more assertions (a fork)

assertThat(x).contains("foo");
piotrek
  • 13,982
  • 13
  • 79
  • 165
  • I did not find a contains method with AssertJ.assertThat. This is what I found instead - org.assertj.core.api.Assertions.assertThat(conversionException).hasMessageContaining("some substring"); – Raj Dec 05 '18 at 01:18
  • sorry, I think my above comment does not suit to the context of this answer. I was on a different use case where I need to check for a substring within an exception message. – Raj Dec 05 '18 at 01:24
11

Use hamcrest Matcher containsString()

// Hamcrest assertion
assertThat(person.getName(), containsString("myName"));

// Error Message
java.lang.AssertionError:
Expected: a string containing "myName"
     got: "some other name"

You can optional add an even more detail error message.

// Hamcrest assertion with custom error message
assertThat("my error message", person.getName(), containsString("myName"));

// Error Message
java.lang.AssertionError: my error message
Expected: a string containing "myName"
     got: "some other name"

Posted my answer to a duplicate question here

Community
  • 1
  • 1
Rylander
  • 19,449
  • 25
  • 93
  • 144
9

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

user2739602
  • 301
  • 5
  • 4
7

Example (junit version- 4.13)

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;

public class TestStr {

@Test
public void testThatStringIsContained(){
    String testStr = "hi,i am a test string";
    assertThat(testStr).contains("test");
 }

}
5

Use the new assertThat syntax together with Hamcrest.

It is available starting with JUnit 4.4.

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
5

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"));
rns
  • 1,047
  • 10
  • 25
2

Another variant is

Assert.assertThat(actual, new Matches(expectedRegex));

Moreover in org.mockito.internal.matchers there are some other interesting matchers, like StartWith, Contains etc.

LEON DENIS
  • 21
  • 2
2

assertj variant

import org.assertj.core.api.Assertions;
Assertions.assertThat(actualStr).contains(subStr);
1

I wrote this utility method

public static void assertContains(String string, String subString) {
    Assertions.assertTrue(string.contains(subString));
}
stakeika
  • 55
  • 1
  • 8
0

I've tried out many answers on this page, none really worked:

  • org.hamcrest.CoreMatchers.containsString does not compile, cannot resolve method.
  • JUnitMatchers.containsString is depricated (and refers to CoreMatchers.containsString).
  • org.hamcrest.Matchers.containsString: NoSuchMethodError

So instead of writing readable code, I decided to use the simple and workable approach mentioned in the question instead.

Hopefully another solution will come up.

P Kuijpers
  • 1,593
  • 15
  • 27
0

The previous answers are fairly good if you are able and willing to add external libraries. For various reasons, this might not be the case. If you can't/don't want to add another dependency to your project, or if you just want to keep hamcrest at arms length, you could use the parts of hamcrest that come with JUnit.

For example, org.hamcrest.BaseMatcher and org.hamcrest.Matcher come with JUnit 4.10. One implementation could be:

public class StringMatchers {
    public static Matcher<String> contains(String expected) {
        return new BaseMatcher<String>() {
            @Override
            public boolean matches(Object actual) {
                String act = (String) actual;
                
                return act.contains(expected);
            }

            @Override
            public void describeTo(Description desc) {
                desc.appendText("should contain ").appendValue(expected);
            }
        };
    }
}

and then you can import it into other test files with import static <package>.StringMatchers.contains. This will leave you with the statement:

assertThat(x, contains(y));

PS. This is suspiciously similar to other libraries, so I would be surprised if they were implemented much different.

src: https://programmingideaswithjake.wordpress.com/2014/11/08/advanced-creation-of-hamcrest-matchers/ **not everything in here works!

Airistotal
  • 95
  • 8