hamcrest

Mockito, JUnit, Hamcrest, Versioning

三世轮回 提交于 2019-12-21 03:20:19
问题 By default, the required version of Hamcrest for: JUnit 4.11 Hamcrest 1.3 Mockito-core 1.9.5 Hamcrest 1.1 There were not insiginifcant API changes between Hamcrest 1.1 and 1.3. Currently my test cases attempt to run JUnit 4.11 with Hamcrest 1.1, but I'm reasonably sure that this is a bad idea. For similar reasons, I suspect that trying to use Mockito-core 1.9.5 with Hamcrest 1.3 is also a bad idea. What to do? Use Hamcrest 1.1 with the latest JUnit and Mockito Use Hamcrest 1.3 with the latest

How to compile Kotlin unit test code that uses hamcrest 'is'

£可爱£侵袭症+ 提交于 2019-12-21 03:14:07
问题 I want to write a unit test for my Kotlin code and use junit/hamcrest matchers, I want to use the is method, but it is a reserved word in Kotlin. How can I get the following to compile? class testExample{ @Test fun example(){ assertThat(1, is(equalTo(1)) } } Currently my IDE, InteliJ is highlighting that as a compilation error, saying it is expecting a ) after is ? 回答1: In Kotlin, is is a reserved word . To get around this you need to escape the code using backticks, so the following will

What is the alternative to using the Deprecated Hamcrest method is()?

主宰稳场 提交于 2019-12-20 10:15:06
问题 I use the following code at the moment to assert on a boolean value, however the method org.hamcrest.Matchers.is() is deprecated. assertThat(someValue, is(false)); Is there a simple alternative syntax to test for boolean values without resorting to assertTrue() which gives you poor failure messages like "java.lang.AssertionError" Edit after receiving comments/answers My initial concerns were raised because Eclipse shows the following import statement as deprecated On viewing the Hamcrest API

How to assertThat something is null with Hamcrest?

跟風遠走 提交于 2019-12-20 09:07:29
问题 How would I assertThat something is null ? for example assertThat(attr.getValue(), is("")); But I get an error saying that I cannot have null in is(null) . 回答1: You can use IsNull.nullValue() method: import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; assertThat(attr.getValue(), is(nullValue())); 回答2: why not use assertNull(object) / assertNotNull(object) ? 回答3: If you want to hamcrest , you can do import static org.hamcrest.Matchers.nullValue; assertThat

How to use Hamcrest to test for exception?

北城余情 提交于 2019-12-19 09:20:42
问题 I have the following code: def f(String s) { assert !s?.contains('.') } What Hamcrest matcher can be used to test the assertion? I know I can use a try / catch block but I prefer keeping the cyclomatic complexity of tests to one. 回答1: EDIT If you REALLY must use Hamcrest, you could write something like: assertThat( { f( 'hi.ho' ) }, thrown( MyException ) ) You will need the ThrownMatcher.thrown(..) matcher which I wrote just for fun. See Gist here. But in Groovy, Hamcrest matchers can be

onChildView and hasSiblings with Espresso

大憨熊 提交于 2019-12-18 03:36:11
问题 I am trying to access a button from a specific view. The same view is displayed 6 times. This is the code I am using. public void testTimeConfig(){ onData(withDesc("description")).onChildView(withId(R.id.positive)).perform(click()); } private static Matcher<Object> withDesc(String desc) { return allOf(is(instanceOf(String.class)), is(desc)); } When I run, I get an error: Error performing 'load adapter data' on view 'is assignable from class: class android.widget.AdapterView'. Is this the best

How to use JUnit and Hamcrest together?

泪湿孤枕 提交于 2019-12-17 17:26:31
问题 I can't understand how JUnit 4.8 should work with Hamcrest matchers. There are some matchers defined inside junit-4.8.jar in org.hamcrest.CoreMatchers . At the same time there are some other matchers in hamcrest-all-1.1.jar in org.hamcrest.Matchers . So, where to go? Shall I explicitly include hamcrest JAR into the project and ignore matchers provided by JUnit? In particular, I'm interested in empty() matcher and can't find it in any of these jars. I need something else? :) And a

Hamcrest: when iterableWithSize fails, it gives a bad message like “got: com.xxx.MyIterClass$1@1970ae0”

我的未来我决定 提交于 2019-12-13 05:43:27
问题 In hamcrest (1.3.RC2, with no JUnit dependencies) I am failing using iterableWithSize() with a SpringDataNeo4j library. I have an (extension of) an Iterator parameterized with Content like this EndResult<Content> contents = contentRepository.findAllByPropertyValue("title", "*"); where EndResult is package org.springframework.data.neo4j.conversion; public interface EndResult extends Iterable {...} and Content is a a @NodeEntity Pojo. With the help of Mark Peters I learned that I should call it

Hamcrest hasItem and Mockito verify inconsistency

自闭症网瘾萝莉.ら 提交于 2019-12-13 04:36:40
问题 I've ran into an issue with hamcrest and mockito. Here is what I'm trying to do: public class A{ public void foo(List<B> arg){ return; } } public BMatcher extends BaseMatcher<B>{ //Some impl... } In my test I want to do something like A a = mock(A.class); B expected = new B(); Mockito.verify(a).foo(argThat(JUnitMatchers.hasItem(new BMatcher(expected))); However, the hasItem matcher returns an Iterable<B> while the foo method expects a List<B> . Is there any good way of verifying the method is

JUnit Assert, Matchers and nested objects

梦想的初衷 提交于 2019-12-13 02:50:03
问题 I have the following collection: Set<DecisionGroup> parentDecisionGroups first of all in my test I need to check that this collection contains two objects with a given ids: assertThat(parentDecisionGroups, hasItem(hasProperty("id", equalTo(decisionGroup1.getId())))); assertThat(parentDecisionGroups, hasItem(hasProperty("id", equalTo(decisionGroup2.getId())))); so far so good... Right now I need to check that parentDecisionGroups.get(0).getOwnerDecision() (where parentDecisionGroup.id ==