Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.both;

public class JUnit_Dummy {

  private StringJoiner joiner;
  private List<String> strings;

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

  @Test
  public void shouldContainBothStringsWhenListIsTwoStrings() {
    strings.add("one");
    strings.add("two");
    assertThat(joiner.join(strings),
        both(containsString("A")).
            and(containsString("B")));
  }

}
_____________

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 (http://junit.sourceforge.net/javadoc/org/junit/matchers/JUnitMatchers.html#containsString(java.lang.String)) 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.

UPDATE:

Here is the exact compiler error:

java: cannot find symbol
  symbol:   method containsString(java.lang.String)
  location: class JUnit_Dummy
like image 519
Richie Thomas Avatar asked Jun 01 '14 21:06

Richie Thomas


People also ask

Why use Hamcrest Matchers?

Purpose of the Hamcrest matcher framework. Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit.

What is Hamcrest Matchers?

Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.


4 Answers

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

import static org.junit.matchers.JUnitMatchers.*;
like image 101
Richie Thomas Avatar answered Oct 15 '22 14:10

Richie Thomas


I faced the same issue with a Spring Boot app. Seems like this is a dependency ordering issue.. one of the dependencies mentioned in pom.xml before the "spring-boot-starter-test" artifact was overriding the hamcrest version.

So all I did was change the order (moved this dependency up):

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

I'm using Spring Boot 1.5.7.RELEASE.

like image 35
Karan Kapoor Avatar answered Oct 15 '22 13:10

Karan Kapoor


We are supposed to use containsString method of hamcrest library.

My suggestion would be to stick to Junit 4 and import hamcrest library 1.3 in your build path. This would do the trick.

This will allow you to access other features of hamcrest library as well.

The solution can also be found by adding the required static imports manually. Or you can configure the required static imports in favorites tab of eclipse.

like image 26
Ishan Arora Avatar answered Oct 15 '22 14:10

Ishan Arora


try this instead

import static org.hamcrest.CoreMatchers.*;

like image 22
mahendra Avatar answered Oct 15 '22 15:10

mahendra