hamcrest

Android Studio - Program type already present: org.hamcrest.CoreMatchers

∥☆過路亽.° 提交于 2019-12-09 10:21:21
问题 I have no idea why this error exists: Program type already present: org.hamcrest.CoreMatchers Message{kind=ERROR, text=Program type already present: org.hamcrest.CoreMatchers, sources=[Unknown source file], tool name=Optional.of(D8)} My code in in the scope if dependency of build.gradle (Module: app) is: dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint

Generics hell: hamcrest matcher as a method parameter

泪湿孤枕 提交于 2019-12-09 07:58:57
问题 So, let's have a list of strings and a function that takes a Hamcrest matcher and returns a result of the matches() method of the provided matcher: public boolean matchIt(final Matcher<? super List<String>> matcher) { final List<String> lst = obtainListFromSomewhere(); return matcher.matches(lst); } So far so good. Now I can easily call: matchIt(empty()); matchIt(anything()); matchIt(hasItem("item")); matchIt(everyItem(equalToIgnoringCase("item"))); ...since all of these factory static

Mockito and Hamcrest: how to verify invocation of Collection argument?

一曲冷凌霜 提交于 2019-12-09 07:26:59
问题 I'm running into a generics problem with Mockito and Hamcrest. Please assume the following interface: public interface Service { void perform(Collection<String> elements); } And the following test snippet: Service service = mock(Service.class); // ... perform business logic verify(service).perform(Matchers.argThat(contains("a", "b"))); So I want to verify that my business logic actually called the service with a collection that contains "a" and "b" in that order. However, the return type of

JMock with(instanceOf(Integer.class)) does not compile in Java 8

一曲冷凌霜 提交于 2019-12-08 05:40:30
问题 After upgrading to Java 8. I now have compile errors of the following kind: The method with(Matcher<Object>) is ambiguous for the type new Expectations(){} It is caused by this method call: import org.jmock.Expectations; public class Ambiguous { public static void main(String[] args) { Expectations expectations = new Expectations(); expectations.with(org.hamcrest.Matchers.instanceOf(Integer.class)); } } It seems like with is returned from instanceOf() is ambiguous from what with() expects, or

NoClassDefFoundError: org/hamcrest/Matchers using PowerMock-OSGi

天涯浪子 提交于 2019-12-08 02:39:40
问题 I get an NoClassDefFoundError for org.hamcrest.Matchers, when i run my Test as OSGi PlugIn test, but when i run it as plain JUnit test everthing works as expected. I am using the OSGi version of PowerMock and have all neccessary dependencies in my launch config. What i am doing wrong? It seems like the Testrunner doesnt see the class, for some reason. Edit: I created a reduced sample project and figured out that the Problem only appear when i use @PrepareForTest(XXX.class) in my class

JMock with(instanceOf(Integer.class)) does not compile in Java 8

断了今生、忘了曾经 提交于 2019-12-07 14:06:30
After upgrading to Java 8. I now have compile errors of the following kind: The method with(Matcher<Object>) is ambiguous for the type new Expectations(){} It is caused by this method call: import org.jmock.Expectations; public class Ambiguous { public static void main(String[] args) { Expectations expectations = new Expectations(); expectations.with(org.hamcrest.Matchers.instanceOf(Integer.class)); } } It seems like with is returned from instanceOf() is ambiguous from what with() expects, or vice versa. Is there a way to fix this? There is some easy ways to help the compiler. assign the

NoClassDefFoundError: org/hamcrest/Matchers using PowerMock-OSGi

一个人想着一个人 提交于 2019-12-06 15:32:45
I get an NoClassDefFoundError for org.hamcrest.Matchers, when i run my Test as OSGi PlugIn test, but when i run it as plain JUnit test everthing works as expected. I am using the OSGi version of PowerMock and have all neccessary dependencies in my launch config. What i am doing wrong? It seems like the Testrunner doesnt see the class, for some reason. Edit: I created a reduced sample project and figured out that the Problem only appear when i use @PrepareForTest(XXX.class) in my class declaration. java.lang.NoClassDefFoundError: org/hamcrest/Matchers at eu.gemtec.commons.util.assertion.Assert

Hamcrest number comparison using between

梦想与她 提交于 2019-12-04 23:36:57
Is there a way in Hamcrest to compare a number within a number range? I am looking for something like this: assertThat(50L, is(between(12L, 1658L))); An alternative to Jeff's solution is to use both : assertThat(50L, is(both(greaterThan(12L)).and(lessThan(1658L)))); I think that's quite readable. You also get a good error message in case the check failed: Expected: is (a value greater than <50L> and a value less than <1658L>) got: <50L> I don't believe between is part of the core hamcrest matchers, but you could do something like this: assertThat(number, allOf(greaterThan(min),lessThan(max)));

TetsNG SoftAssert with Hamcrest matcher

戏子无情 提交于 2019-12-04 16:44:32
I want to use a Hamcrest matcher inside a TestNG test and with a soft assert specifically. How can I do this? I know that I can use Hamcrest's assertions inside a test like: assertThat(actual, containsInAnyOrder(expected)); But I can't understand how can I use TestNG soft assert method like this one: SoftAssert softAssert = new SoftAssert(); together with a Hamcrest matcher. Because I can't invoke the Hamcrest's assertThat on TestNG's softAssert like softAssert.assertThat(...) So, what is the correct way to use a Hamcrest matcher together with TestNG? To the best of my knowledge, you cannot

Hamcrest with MockMvc: check that key exists but value may be null

假装没事ソ 提交于 2019-12-04 10:40:27
问题 I'm doing some tests with MockMvc, and I want to validate the structure of a JSON response. Specifically, I want to make sure that the key to an attribute exists, and that the value is of a certain type or null. { "keyToNull": null, # This may be null, or a String "keyToString": "some value" } The following works for me, but I'm wondering if there's a way to combine each group of two expectations into a single line, as I have a lot of attributes to check: import static org.springframework