JUnit assert contains for strings
Codemia Knowledge Hub ·
JUnit has no built-in assertContains for strings. Assert that actual.contains(expected) is true, or use a richer assertion library such as AssertJ or Hamcrest for more expressive failure messages.
assertTrue with String.contains
The simplest JUnit-only version:
This is valid, keeps the dependency list small and works for many tests. Its weakness is the failure output, which is less descriptive than a purpose-built string assertion.
Adding a failure message
When the substring check is central to the test, include a message that shows what was expected:
The supplier form builds the message lazily and makes failures much easier to diagnose.
Checking for null separately
If the string could be null, assert that first so the failure tells you what actually went wrong:
This is clearer than writing response != null && response.contains("token") inside one assertion.
AssertJ for readable assertions
If your project already uses AssertJ or Hamcrest, containment checks read better. With AssertJ:
This reads closer to English and usually produces better failure messages. JUnit is still the test runner; AssertJ only provides the assertions.
Ignoring case
String.contains is case-sensitive. If that is not what you want, normalize both sides or use an assertion library feature that says so explicitly:
Using Locale.ROOT avoids locale-dependent casing surprises.
When containment is the wrong check
Sometimes a substring assertion stands in for what you really want:
- equality
- a prefix or suffix check
- a regex or pattern match
- structured data validation such as JSON parsing
For example, if the response is JSON, checking contains("\"status\":\"ok\"") is usually weaker than parsing the JSON and asserting the actual field value.
Use containment when the test is genuinely about substring presence, not because it is the fastest thing to type.
Related reading
- Async API endpoint Spring Webflux
- Async Aspect AfterReturnung. My method has HttpServletRequest object as method argument, but able to access it my aop method
- Async communication between spring boot micro services
- Async not working for method having return type void
- assertEquals vs. assertEqual in python
- Asserting successive calls to a mock method
- Async not working in Spring API rest with Interfaces
- Async Spring Controllers vs normal Controllers
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.