toomanyrichies toomanyrichies - 1 year ago 475
Java Question

Why is my import of the containsString method not working?

I've written the simple Java script below in order to learn more about TDD, IntelliJ and Java itself.

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.both;

public class JUnit_Dummy {

private StringJoiner joiner;
private List<String> strings;

public void setUp() throws Exception {
strings = new ArrayList<String>();
joiner = new StringJoiner();

public void shouldContainBothStringsWhenListIsTwoStrings() {


import java.util.List;

public class StringJoiner {
public String join(List<String> strings) {
if(strings.size() > 0) {
return (strings.get(0);
return "";

I'm trying to use the "containsString" method inside an assertion, but IntelliJ keeps telling me that it "cannot resolve method 'containsString(java.lang.String)". This despite the fact that the jUnit docs ( tell me that this method does accept a String parameter.

I've tried swapping out various import statements, including the following:

import static org.hamcrest.Matcher.containsString;
import static org.hamcrest.Matcher.*;
import static org.hamcrest.CoreMatchers.*;

The best that I get is a greyed-out import statement telling me that the import statement is unused. Not sure what the problem is, any help would be appreciated.


Here is the exact compiler error:

java: cannot find symbol
symbol: method containsString(java.lang.String)
location: class JUnit_Dummy

Answer Source

I thought I had tried every worthwhile import statement already, but this one did the trick:

import static org.junit.matchers.JUnitMatchers.*;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download