ChatGPT解决这个技术问题 Extra ChatGPT

AssertContains on strings in jUnit

Is there a nicer way to write in jUnit

String x = "foo bar";
Assert.assertTrue(x.contains("foo"));
IMO this is nice enough, suggested options are less readable
@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)
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.
The question itself should be the accepted answer :D

m
masterxilo

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;

Be sure you're using org.junit.Assert versus junit.framework.Assert, as the latter doesn't have the Hamcrest Matcher assertThat()
I think when running JUnit 4.10, the class to use is org.junit.matchers.JUnitMatchers, e.g.: assertThat("something", JUnitMatchers.containsString("some"));
The failure message for a failing assertThat is way more helpful then an assertTrue
static imports needed are import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.containsString; - just to save someone from trouble
... and org.hamcrest.Matchers.containsString; in the latest api, in the hamcrest-library dependency.
p
piotrek

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

assertThat(x).contains("foo");

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");
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.
C
Community

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


R
Robert Munteanu

Use the new assertThat syntax together with Hamcrest.

It is available starting with JUnit 4.4.


r
rns

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

u
user2739602

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


M
Muriithi Derrick

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

}

This should be the accepted answer. Easiest and works
Thanks @ajpieri.
L
LEON DENIS

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.


L
Lazarenko Alex

assertj variant

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

P
P Kuijpers

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.