How to count RecyclerView items with Espresso

Here an example ViewAssertion to check RecyclerView item count public class RecyclerViewItemCountAssertion implements ViewAssertion { private final int expectedCount; public RecyclerViewItemCountAssertion(int expectedCount) { this.expectedCount = expectedCount; } @Override public void check(View view, NoMatchingViewException noViewFoundException) { if (noViewFoundException != null) { throw noViewFoundException; } RecyclerView recyclerView = (RecyclerView) view; RecyclerView.Adapter adapter = recyclerView.getAdapter(); assertThat(adapter.getItemCount(), is(expectedCount)); } … Read more

Difference between hamcrest-library Matchers and hamcrest-core CoreMatchers

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 for just these matchers. The other matchers are in the “library” module grouped by the types of objects they match and are optional. org.hamcrest.Matchers includes both sets … Read more

Is there a Hamcrest “for each” Matcher that asserts all elements of a Collection or Iterable match a single specific Matcher?

Use the Every matcher. import org.hamcrest.beans.HasPropertyWithValue; import org.hamcrest.core.Every; import org.hamcrest.core.Is; import org.junit.Assert; Assert.assertThat(people, (Every.everyItem(HasPropertyWithValue.hasProperty(“gender”, Is.is(“male”))))); Hamcrest also provides Matchers#everyItem as a shortcut to that Matcher. Full example @org.junit.Test public void method() throws Exception { Iterable<Person> people = Arrays.asList(new Person(), new Person()); Assert.assertThat(people, (Every.everyItem(HasPropertyWithValue.hasProperty(“gender”, Is.is(“male”))))); } public static class Person { String gender = “male”; public … Read more

How do Hamcrest’s hasItems, contains and containsInAnyOrder differ?

hasItems checks: consecutive passes over the examined Iterable yield at least one item that is equal to the corresponding item from the specified items. That is, it makes sure that the collections contains at least these items, in any order. So, assertThat(c, hasItems(“one”, “two”)); would also pass, with the extra item being ignored. And: assertThat(c, … Read more

How to use JUnit and Hamcrest together?

If you’re using a Hamcrest with a version greater or equal than 1.2, then you should use the junit-dep.jar. This jar has no Hamcrest classes and therefore you avoid classloading problems. Since JUnit 4.11 the junit.jar itself has no Hamcrest classes. There is no need for junit-dep.jar anymore.

Hamcrest compare collections

If you want to assert that the two lists are identical, don’t complicate things with Hamcrest: assertEquals(expectedList, actual.getList()); If you really intend to perform an order-insensitive comparison, you can call the containsInAnyOrder varargs method and provide values directly: assertThat(actual.getList(), containsInAnyOrder(“item1”, “item2”)); (Assuming that your list is of String, rather than Agent, for this example.) If … Read more

How do I assert an Iterable contains elements with a certain property?

Thank you @Razvan who pointed me in the right direction. I was able to get it in one line and I successfully hunted down the imports for Hamcrest 1.3. the imports: import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.contains; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.beans.HasPropertyWithValue.hasProperty; the code: assertThat( myClass.getMyItems(), contains( hasProperty(“name”, is(“foo”)), hasProperty(“name”, is(“bar”)) ));

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)