Wednesday, 15 July 2015

android espresso onData


Solution 1

The easy version


onData(allOf(is(instanceOf(DeliveryVO.class)), hasProperty("deliveryReferenceId", is("1235"))))
        .perform(click());




Solution 2

Or you can do in this way: (which can be reused, highly recommend this way.)



onData(withDeliveryReferenceId("1235")).perform(click());




public Matcher<Object> withDeliveryReferenceId(String expectedText) {
    checkNotNull(expectedText);
    return new DeliveryVOMatch(expectedText);
}

class DeliveryVOMatch extends BoundedMatcher<Object, DeliveryVO>{
    private final String deliveryReferenceId;

    public DeliveryVOMatch(String deliveryReferenceId) {
        super(DeliveryVO.class);
        this.deliveryReferenceId = deliveryReferenceId;
    }

    @Override    public boolean matchesSafely(DeliveryVO deliveryVO) {
        return hasProperty("deliveryReferenceId", equalTo(deliveryReferenceId)).matches(deliveryVO);
    }

    @Override    public void describeTo(Description description) {
        description.appendText("with DeliveryVO deliveryReferenceId : " + deliveryReferenceId);
    }
}

No comments:

Post a comment