草庐IT

Hamcrest

全部标签

java - JUnit Matcher#startsWith 的声明在哪里?

我正在浏览junitExpectedExceptions'javadoc我无法理解他们示例中的startsWith来自哪里(在代码中标记为HERE)。我检查了CoreMatcherutilityclass但找不到任何静态startsWith方法。该方法位于何处?(我当然可以自己写,但这不是重点)publicstaticclassHasExpectedException{@RulepublicExpectedExceptionthrown=ExpectedException.none();@TestpublicvoidthrowsNullPointerExceptionWithMessa

java - 使用hamcrest匹配Map包含不同类型的条目

假设我有一张map:Mapmap1=newHashMap();map1.put("foo1","foo1");map1.put("foo2",Arrays.asList("foo2","bar2"));现在我想使用Hamcrest匹配器来验证map的值。如果这是一个Map我会做类似的事情:assertThat(map1,hasEntry("foo1","foo1"));但是,当我尝试将其与Map一起使用时,我遇到了困难,因为Map中的条目可能是字符串或值列表。这适用于第一个条目:assertThat(map1,hasEntry("foo1",(Object)"foo1"));对于第二个

java - 断言复杂对象时的 Junit 最佳实践

最近我正在为遗留系统编写大量JUnit测试。我经常遇到这样的问题:断言复杂对象的最佳方式是什么?这是我当前的代码publicclassSomeParserTest{@TestpublicvoidtestParse()throwsException{finalSomeParsersomeParser=newSomeParser();someParser.parse("stringfromsomefile");finalListlistOfResults=someParser.getResults();assertThat(listOfResults,hasSize(5));assertR

java - 如何在 JUnit 中比较两个 double 列表

在JUnit4测试中,我有一个方法getValues()返回List我想与引用列表进行比较的对象。到目前为止,我找到的最佳解决方案是使用org.hamcrest.collection.IsArray.hasItems和org.hamcrest.Matchers.closeTo像这样:assertThat(getValues(),hasItems(closeTo(0.2,EPSILON),closeTo(0.3,EPSILON)));这对于只返回少量值的测试来说很顺利。但如果测试返回更多值,这绝对不是最佳方法。我也试过下面的代码。垂头丧气到Matcher之前hasItems代码编译需要:

java - 在具有特定属性的列表中仅存在一个项目的 Hamcrest 测试

通过Hamcrest,我们可以轻松地测试列表中是否存在至少一个具有特定属性的项目,例如ListmyList=....MatcherAssert.assertThat(myList,Matchers.hasItem(Matchers.hasProperty("fieldName",Matchers.equalTo("Afunnystring")))));类Pojo是这样的:publicclassPojo{privateStringfieldName;}这很好,但是我如何检查列表中是否恰好有一个对象具有特定的属性? 最佳答案 您可能必须

java - 如何断言 map 包含带有条目的 map

我有一个单元测试需要检查嵌套映射值。我可以通过拉出条目并匹配底层Map来使我的断言起作用,但我一直在寻找一种清晰的方式来展示断言的作用。这是一个非常简化的测试:importstaticorg.hamcrest.MatcherAssert.assertThat;importstaticorg.hamcrest.Matchers.hasEntry;importjava.util.HashMap;importjava.util.Map;importorg.junit.Test;publicclassMapContainsMapTest{@TestpublicvoidtestMapHasMap

java - 滥用 hamcrest hasItems

我有一个整数列表(当前),我想检查这个列表是否包含预期列表中的所有元素,甚至不包含列表notExpected中的一个元素,所以代码如下:Listexpected=newArrayList();expected.add(1);expected.add(2);ListnotExpected=newArrayList();notExpected.add(3);notExpected.add(4);Listcurrent=newArrayList();current.add(1);current.add(2);assertThat(current,not(hasItems(notExpecte

java - 实现 Spring Controller 的单元测试时缺少依赖项 hasSize() 和 hasProperty()

我正尝试在SpringMVCController中实现一个方法的单元测试,如下所示:@TestpublicvoidtestGetProfile(){PersonmockPerson=newPerson();mockPerson.setPersonId(1);mockPerson.setName("MrBrown");mockPerson.setAddress("Somewhere");mockPerson.setTelephone("1234567890");mockPerson.setEmail("brown@brown.com");when(mockPersonService.ge

java - Hamcrest assertThat 模棱两可?

我从一所大学获得了一些示例代码,导入了项目并尝试运行测试:方法assertThat(Integer,Matcher)对于MyClass类型是不明确的每个assertThat都被标记为红色并带有相同的错误消息,所以我尝试编写描述问题的最简单的测试:importstaticorg.hamcrest.MatcherAssert.*;importstaticorg.hamcrest.Matchers.*;importstaticorg.junit.Assert.*;@TestpublicvoidwhenAssertThatThenItIsAmbiguous(){Listlist=Arrays.

java - 是否有任何工具使用 hamcrest Factory 注释?

我今天坐下来写一个匹配器,并决定快速浏览一下jmock文档以刷新我对该过程的内存,并注意到对org.hamcrest.Factory注释的引用。注释状态的文档。MarksaHamcreststaticfactorymethodsotoolsrecognisethem.Afactorymethodisanequivalenttoanamedconstructor.是否有任何工具实际使用此注释? 最佳答案 如theHamcresttutorial中所述,Factory注释被Hamcrest代码生成器org.hamcrest.genera