hamcrest

Mockito equivalent to this Hamcrest “samePropertyValuesAs”/jMock “with” idiom?

こ雲淡風輕ζ 提交于 2019-12-11 05:48:41
问题 Hamcrest/jMock code looks like this: @Test public void setsSniperValuesInColumns() { context.checking(new Expectations() {{ one(listener).tableChanged(with(aRowChangedEvent())); }}); model.sniperStatusChanged(new SniperState("item id", 555, 666), MainWindow.STATUS_BIDDING); ... } private Matcher<TableModelEvent> aRowChangedEvent() { return samePropertyValuesAs(new TableModelEvent(model, 0)); } NB this is taken from "Growing Object-Oriented Software Guided by Tests" (p. 157). The authors of

Strict matching in hamcrest?

£可爱£侵袭症+ 提交于 2019-12-11 01:47:43
问题 I'm trying to use Hamcrest, but constantly run into the following: Hamcrest matchers are shortcircuited, so for eg if I write: Assert.assertThat(list, everyItem(not(isIn(shouldNotBeInList)))); Just the first faulty element of shouldNotBeInList is reported. I expect tests to tell me as much, as possible. Could I write assertions in hamcrest, that they report nicely, so that all mismatches are reported, or should I create my own matchers or use another library? Example output for List<String>

Does matching with argThat only work for methods with a single argument?

社会主义新天地 提交于 2019-12-11 01:05:33
问题 I have the following method invocation in my class under test: class ClassUnderTest { [...] restTemplate.getForEntity(url, MyResponseEntity.class, parameterMap); [...] } In the unit test of the class containing this line, I want to stub the getForEntity method such that it returns different responses for different entries in the parameterMap . Using BDDMockito, I'm trying to match that the desired parameter is part of the parameterMap by using argThat: @InjectMocks private ClassUnderTest

Strange AllOf hamcrest matcher mismatch description

为君一笑 提交于 2019-12-10 17:44:13
问题 I'm using hamcrest 1.3 . Its implementation of matches(Object o, Description mismatch) looks like that: @Override public boolean matches(Object o, Description mismatch) { for (Matcher<? super T> matcher : matchers) { if (!matcher.matches(o)) { mismatch.appendDescriptionOf(matcher).appendText(" "); matcher.describeMismatch(o, mismatch); return false; } } return true; } When describing mismatch, it first appends the description of matcher that failed, and only then the actual mismatch. This

Eclipse: The type org.hamcrest.core.CombinableMatcher$CombinableBothMatcher cannot be resolved. It is indirectly referenced from required .class files

微笑、不失礼 提交于 2019-12-10 15:38:43
问题 I was working on a unit testing for a Spring MVC controller using TestNG and Mockito . I've included Hamcrest library in the Maven dependencies as shown below. What am I missing here? The error shows up when I use the following two methods: org.hamcrest.Matchers.hasSize; org.hamcrest.Matchers.is; The following are my dependencies: <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org

Hamcrest assertion of boolean fields with an “is” prefix getter method

雨燕双飞 提交于 2019-12-10 14:53:40
问题 We would like to assert that a list of custom objects contains an object with some of its fields having certain values, with a series of assertions like this assertThat(customObjectList, hasItem(hasProperty("someField", equalTo(someValue)))); However the custom object has also boolean type fields, where the getter method has an "is" prefix instead of "get", and there the assertion seems to fail with java.lang.AssertionError: Expected: a collection containing hasProperty("booleanField", <true>

Hamcrest Matchers - Assert Type of List

点点圈 提交于 2019-12-10 14:39:30
问题 The Problem I'm currently trying to use Hamcrest Matchers to assert that the list type being returned is of a specific type. For example, let's say I have the following List that is being returned by my service call: List<SomePOJO> myList; I want to assert that the list being returned is parametrized of type SomePOJO and not TheOtherPOJO . However, it appears that Hamcrest Matchers does not have this sort of functionality. What I Have Tried After some research, I have seen the following

Hamcrest Matchers.contains matcher not working (?)

亡梦爱人 提交于 2019-12-10 12:59:43
问题 I am trying to test if a collection has an item which toString() method returns a particular String. I tried do it using excellent Hamcrest matching classes, by combining contains with Matchers.hasToString , but somehow, its Matchers.contains is not being able to match an item even though it is present in the collection. Here's an example: class Item { private String name; public Item(String name){ this.name = name; } public String toString(){ return name; } } // here's a sample collection,

How to use Mockito/Hamcrest in unit tests in Android Studio

白昼怎懂夜的黑 提交于 2019-12-09 18:25:31
问题 I want to be able to make unit tests and instrumentation tests in Android Studio, and using Mockito in them. I'm using the new approach for tests in Android Studio 0.8. This is: building with gradle using official Android API for testing (ActivityInstrumentationTestCase2, etc) having the tests inside the directory of the app, not as a separate module launching the tests in Android Studio as a "Android Test" run configuration How can I write code in my tests that depends on libraries used only

Want a JUnitMatchers AssertThat to test string contains 3 or more sub strings (currently using assertThat … both … and …)

不问归期 提交于 2019-12-09 15:18:31
问题 import static org.junit.matchers.JUnitMatchers.both; import static org.junit.matchers.JUnitMatchers.containsString; Now I check it contains foo and bar as below ... Assert.assertThat(text, both(containsString("foo")). and(containsString("bar"))); What is cleanest way to test also check it contains 3 or more strings e.g. 'foo', 'bar' and 'baz' ? 回答1: Use AllOf Assert.assertThat(test, CoreMatchers.allOf( containsString("foo"), containsString("bar"), containsString("bar2"), containsString("ba3")