hamcrest

Difference between hamcrest-library Matchers and hamcrest-core CoreMatchers

混江龙づ霸主 提交于 2019-12-04 08:46:45
问题 It looks like the hamcrest org.hamcrest.Matchers class is very similar to org.hamcrest.CoreMatchers (though it looks like Matchers has more). Why would I choose to use CoreMatchers (other than it looks like the class is slightly smaller), and why are these two classes so similar? 回答1: The Hamcrest matchers are split into several modules. The "core" includes the most basic matchers and abstract classes required for building other matchers. org.hamcrest.CoreMatchers includes the factory methods

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

做~自己de王妃 提交于 2019-12-04 07:42:22
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 for the tests, such as mockito or hamcrest? I'd like to include these libraries when compiling and

How to use (primitive) autoboxing/widening with Hamcrest?

可紊 提交于 2019-12-04 03:04:11
问题 I came across https://code.google.com/p/hamcrest/issues/detail?id=130 to add some sugar syntax for Hamcrest matchers. But the idea was rejected by the Hamcrest developers. Any other smart ideas to make tests better readable by avoiding having to type L behind longs? @Test public void test1() { int actual = 1; assertThat(actual, is(1)); } @Test public void test2() { long actual = 1L; assertThat(actual, is(1)); // fails as expected is <1> but result was <1L> // assertThat(actual, is(1L)); off

Which dependencies do I need to use Mockito and JUnit in an Eclipse RCP Tycho project

☆樱花仙子☆ 提交于 2019-12-04 02:29:43
This is my current test fragment: <packaging>eclipse-test-plugin</packaging> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>com.springsource.org.junit</artifactId> <version>4.7.0</version> </dependency> </dependencies> with the following plugins configuration: <plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-surefire-plugin</artifactId> <version>${tycho.version}</version> <configuration> <dependencies> <dependency> <type>p2-installable-unit</type> <artifactId>org.eclipse.equinox.ds</artifactId> </dependency> <dependency> <type>p2-installable-unit</type>

Testing in Hamcrest that exists only one item in a list with a specific property

只愿长相守 提交于 2019-12-04 02:16:39
With Hamcrest we can easily test that exists at least one item in a list with a specific property, e.g. List<Pojo> myList = .... MatcherAssert.assertThat(myList, Matchers.hasItem(Matchers.<Pojo>hasProperty("fieldName", Matchers.equalTo("A funny string"))))); where the class Pojo is something like: public class Pojo{ private String fieldName; } That's nice, but how can I check that there is exactly one object in the list with the specificed properties? You might have to write your own matcher for this. (I prefer the fest assertions and Mockito, but used to use Hamcrest...) For example... import

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

陌路散爱 提交于 2019-12-04 01:57:45
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' ? Use AllOf Assert.assertThat(test, CoreMatchers.allOf( containsString("foo"), containsString("bar"), containsString("bar2"), containsString("ba3"))); I don't know a elegant way in pure JUnit but you could take a look at Fixtures for Easy Software Testing

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

可紊 提交于 2019-12-03 22:21:47
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 ? In Kotlin, is is a reserved word . To get around this you need to escape the code using backticks, so the following will allow you to compile the code: class testExample{ @Test fun example(){ assertThat(1, `is`(equalTo(1)) } } David

Hamcrest - what version to use? 1.3 or 2

别等时光非礼了梦想. 提交于 2019-12-03 22:06:43
I am quite confused. Currently I am testing my spring application using <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> I was happy as long as I wanted to match RegularExpressions. In hamcrest 1.3 you need to write your own matcher, which I did not like that much. I searched and found that hamcrest 2.0 has something build in, like: assertThat(DateHelper.getActualDateForXML(), MatchesPattern.matchesPattern("\\d{4}+-\\d{2}-+\\d{2}+T\\d{2}+:\\d{2}+:\\d{2}+")); I was happy, I added: <dependency> <groupId

Map equality using Hamcrest

我的梦境 提交于 2019-12-03 14:19:50
问题 I'd like to use hamcrest to assert that two maps are equal, i.e. they have the same set of keys pointing to the same values. My current best guess is: assertThat( affA.entrySet(), hasItems( affB.entrySet() ); which gives: The method assertThat(T, Matcher) in the type Assert is not applicable for the arguments (Set>, Matcher>>>) I've also looked into variations of containsAll, and some others provided by the hamcrest packages. Can anyone point me in the right direction? Or do I have to write a

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

我是研究僧i 提交于 2019-12-03 13:46:37
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.test.web.servlet.result.MockMvcResultMatchers.*; import static org.hamcrest.Matchers.*; .andExpect