Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我导入的containsString方法不起作用?_Java_Junit_Intellij Idea - Fatal编程技术网

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

Java 为什么我导入的containsString方法不起作用?,java,junit,intellij-idea,Java,Junit,Intellij Idea,为了进一步了解TDD、IntelliJ和Java本身,我编写了下面的简单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

为了进一步了解TDD、IntelliJ和Java本身,我编写了下面的简单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 "";
  }
}
我得到的最好结果是一个灰显的import语句,告诉我import语句未使用。不确定问题出在哪里,如果有任何帮助,我们将不胜感激

更新:

以下是确切的编译器错误:

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

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

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

我们应该使用hamcrest库的
containsString
方法

我的建议是坚持JUnit4并在构建路径中导入hamcrest库1.3。这样就行了

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


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

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

因此,我所做的只是更改顺序(将此依赖项上移):


org.springframework.boot
弹簧起动试验
测试
我使用的是Spring Boot 1.5.7.RELEASE。

试试这个

导入静态org.hamcrest.CoreMatchers.*


我正在和MAVEN合作——做一个教程,我遇到了同样的问题

我使用了“importstaticorg.hamcrest.CoreMatchers.*”;解决方案,但失败了


因此,我将JUNIT移到POM文件列表中的第一位,这就解决了问题。

请提供您可以看到的确切编译器错误和/或堆栈跟踪。添加。但是fwiw,作为一个IDE,IntelliJ能够告诉我,如果我不编译它,它就无法解析有问题的方法。JUnitMatchers还是CoreMatchers?@SotiriosDelimanolis问得好。我想我已经尝试了这两种方法,但我只是尝试了以下方法,效果很好:“导入static org.junit.matchers.JUnitMatchers.*;”感谢您的启发。:-)@SotiriosDelimanolis,你知道为什么JUnitMatchers会工作而CoreMatchers不会工作吗?我认为你应该使用
org.hamcrest.Matchers.containssString
,因为你的导入已经被正式否决了(注意
Matchers
中的
s
)。但是我注意到还有
org.junit.internal.matchers.StringContains.containsString
org.hamcrest.core.StringContains.containsString
,所以我想知道最规范的导入是什么。也许有人有答案?虽然这个导入确实解决了这个问题,但问题仍然是“为什么?”与Spring Boot 2.2.4.RELEASE相同
import static org.junit.matchers.JUnitMatchers.*;
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>