Java 为什么我导入的 containsString 方法不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23984756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:33:28  来源:igfitidea点击:

Why is my import of the containsString method not working?

javajunitintellij-idea

提问by Richie Thomas

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

我编写了下面的简单 Java 脚本,以了解有关 TDD、IntelliJ 和 Java 本身的更多信息。

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.

我试图在断言中使用“containsString”方法,但 IntelliJ 一直告诉我它“无法解析方法 'containsString(java.lang.String)”。尽管事实上 jUnit 文档(http://junit.sourceforge.net/javadoc/org/junit/matchers/JUnitMatchers.html#containsString(java.lang.String))告诉我这个方法确实接受一个 String 参数.

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.

我得到的最好的是一个灰色的 import 语句,告诉我 import 语句未使用。不确定问题是什么,任何帮助将不胜感激。

UPDATE:

更新:

Here is the exact compiler error:

这是确切的编译器错误:

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

采纳答案by Richie Thomas

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

我以为我已经尝试了所有有价值的 import 语句,但是这个成功了:

import static org.junit.matchers.JUnitMatchers.*;

回答by user_007

We are supposed to use containsStringmethod of hamcrest library.

我们应该使用containsStringhamcrest库的方法。

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

我的建议是坚持使用 Junit 4 并在您的构建路径中导入 hamcrest 库 1.3。这可以解决问题。

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

这也将允许您访问 hamcrest 库的其他功能。

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.

也可以通过手动添加所需的静态导入来找到解决方案。或者您可以在 eclipse 的收藏夹选项卡中配置所需的静态导入。

回答by Karan Kapoor

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.

我在 Spring Boot 应用程序中遇到了同样的问题。似乎这是一个依赖项排序问题..在“spring-boot-starter-test”工件覆盖 hamcrest 版本之前,pom.xml 中提到的依赖项之一。

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.

我正在使用 Spring Boot 1.5.7.RELEASE。

回答by mahendra

try this instead

试试这个

import static org.hamcrest.CoreMatchers.*;

导入静态 org.hamcrest.CoreMatchers.*;