修复消息"The method empty() is undefined for the type JunitAssertions"



我正在使用Junit断言。我在使用assertThat时遇到了问题。下面的代码显示'assertThat'已被删除。此外,无论我使用什么匹配器,如empty, is, equalsTo,我都会得到错误消息为"方法empty()对于类型JunitAssertions是未定义的"。我的项目使用的Junit版本是4.11。

我的问题是使用assertThat断言需要做些什么?

从我的项目复制的代码:

package BasicsOfJava.BasicsOfJava;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.testng.Assert.assertEquals;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class JunitAssertions {
@Test
public void assertionsInJava()
{   
List<String> emptyList = new ArrayList<>();
**assertThat**(emptyList, empty());
Note : In my editor, assertThat is struck.      

}

assertThat是Hamcrest的断言方法。JUnit决定不再支持这些断言,因为Hamcrest提供了自己的assertThat(参见PR 1150)。对您来说,这意味着添加导入

import static org.hamcrest.MatcherAssert.assertThat

和删除

import static org.junit.Assert.*

如果你只使用Hamcrest断言。您还需要为像empty()这样的匹配器做一个静态导入。基本匹配器属于CoreMatchers

import static org.hamcrest.Matchers.*;

相关内容

最新更新