草庐IT

Hamcrest

全部标签

java - hamcrest hasItem 和 hasProperty,断言是否存在具有属性值的对象

importstaticorg.hamcrest.MatcherAssert.assertThat;importstaticorg.hamcrest.Matchers.hasItem;importstaticorg.hamcrest.Matchers.equalTo;assertThat(actual,hasItem(hasProperty("id",equalTo(1L))));其中actual是一个ID为Long的POJO。我明白了,ThemethodassertThat(T,Matcher)inthetypeMatcherAssertisnotapplicableforthear

java - Hamcrest - 使用什么版本? 1.3 或 2

我很困惑。目前我正在使用测试我的spring应用程序org.springframework.bootspring-boot-starter-testtest只要我想匹配正则表达式,我就很高兴。在hamcrest1.3中,您需要编写自己的匹配器,我不太喜欢它。我搜索并发现hamcrest2.0内置了一些东西,例如:assertThat(DateHelper.getActualDateForXML(),MatchesPattern.matchesPattern("\\d{4}+-\\d{2}-+\\d{2}+T\\d{2}+:\\d{2}+:\\d{2}+"));我很高兴,我补充说:org

java - 如何使用 Hamcrest 检查集合是否包含给定顺序的项目

如果给定的集合包含给定顺序的给定项目,如何使用Hamcrest检查?我试过hasItems但它只是忽略了顺序。Listlist=Arrays.asList("foo","bar","boo");assertThat(list,hasItems("foo","boo"));//Iwantthistofail,becausetheorderisdifferentthanin"list"assertThat(list,hasItems("boo","foo")); 最佳答案 您可以改用contains匹配器,但您可能需要使用最新版本的Ha

java - hamcrest 测试总是失败

我正在使用hamcrest1.3来测试我的代码。这简直就是一个死。我正在尝试对其进行测试以确保生成的数字小于13。我有一个打印语句打印生成的数字是什么。生成的数字始终小于13,但测试始终失败。是不是我做错了什么?这是我正在测试的代码。importjava.util.Random;publicclassDie{privateintnumSides;Randomrand;publicDie(intnumSides){this.numSides=numSides;rand=newRandom(System.currentTimeMillis());}publicintroll(){retur

java - 如何一起使用 JUnit 和 Hamcrest?

我不明白JUnit4.8应该如何与Hamcrest匹配器一起工作。junit-4.8.jar内部定义了一些匹配器在org.hamcrest.CoreMatchers中。同时,hamcrest-all-1.1.jar中还有一些other匹配器。在org.hamcrest.Matchers中。那么,去哪里呢?我是否应该在项目中明确包含hamcrestJAR并忽略JUnit提供的匹配器?特别是,我对empty()匹配器很感兴趣,但在这些jar中都找不到它。我需要别的东西吗?:)还有一个哲学问题:为什么JUnit将org.hamcrest包包含在自己的发行版中,而不是鼓励我们使用原始的hamc

java - 如何用 Hamcrest 断言某事为空?

我怎么会assertThat某事是null?例如assertThat(attr.getValue(),is(""));但我收到一条错误消息,提示我在is(null)中不能有null。 最佳答案 您可以使用IsNull.nullValue()方法:importstaticorg.hamcrest.Matchers.is;importstaticorg.hamcrest.Matchers.nullValue;assertThat(attr.getValue(),is(nullValue()));

android - 错误 JSON.simple : java. util.zip.ZipException : duplicate entry: org/hamcrest/BaseDescription. 类

在添加JSON.simple并启用MultiDex后,我在androidstudio中遇到问题并收到以下错误:Error:Executionfailedfortask':app:packageAllDebugClassesForMultiDex'.java.util.zip.ZipException:duplicateentry:org/hamcrest/BaseDescription.class这是我的build.gradle:applyplugin:'com.android.application'android{compileSdkVersion23buildToolsVersi

java.lang.NoClassDefFoundError : org/hamcrest/SelfDescribing

在eclipse中运行junit测试时,我得到了这个Exception:java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing我已经添加了junit.jar库文件。我尝试过不同版本的junit.jar:4.4、4.8等如何解决这个异常? 最佳答案 将hamcrest-all-X.X.jar添加到您的类路径。截至2015年2月的最新版本是1.3:http://code.google.com/p/hamcrest/downloads/detail?name=hamcrest-

java.lang.NoClassDefFoundError : org/hamcrest/SelfDescribing

在eclipse中运行junit测试时,我得到了这个Exception:java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing我已经添加了junit.jar库文件。我尝试过不同版本的junit.jar:4.4、4.8等如何解决这个异常? 最佳答案 将hamcrest-all-X.X.jar添加到您的类路径。截至2015年2月的最新版本是1.3:http://code.google.com/p/hamcrest/downloads/detail?name=hamcrest-

java - 检查 Hamcrest 中的 List 是否为空

我想知道是否有人知道使用assertThat()和Matchers检查列表是否为空的方法?我能看到的最好的方法就是使用JUnit:assertFalse(list.isEmpty());但我希望在Hamcrest有某种方法可以做到这一点。 最佳答案 总会有的assertThat(list.isEmpty(),is(false));...但我猜这不是你的意思:)或者:assertThat((Collection)list,is(not(empty())));empty()是Matchers类中的静态变量。请注意需要将list转换为Co