Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hamcrest matcher for checking return value of method in collection

Tags:

java

hamcrest

hasProperty can be used with hasItem to check for the value of a given property, eg:

Matcher hasName = Matchers<Person>hasProperty("name", is("Winkleburger"));
assertThat(names, hasItem(hasName));

This is fine when name is a property, ie: there is a method called getName().

Is there a matcher that will check a method that isn't a property? ie: in this case, it will check the return value of the method name() rather than getName(), for items in the collection.

like image 261
tekumara Avatar asked Sep 03 '14 13:09

tekumara


People also ask

Is Hamcrest a matcher?

Overview. Hamcrest is the well-known framework used for unit testing in the Java ecosystem. It's bundled in JUnit and simply put, it uses existing predicates – called matcher classes – for making assertions.

Which Hamcrest matcher is just like the && operator?

Which Hamcrest matcher is just like the && operator? Explanation: Checks to see if all contained matchers match (just like the && operator). 7.

Is assertThat actual is Equalto expected ))) a valid Hamcrest assert statement?

assertEquals() is the method of Assert class in JUnit, assertThat() belongs to Matchers class of Hamcrest. Both methods assert the same thing; however, hamcrest matcher is more human-readable. As you see, it is like an English sentence “Assert that actual is equal to the expected value”.


2 Answers

You can use another Hamcrest built-in for this, a FeatureMatcher. These are designed to be combined with other matchers after they transform your input into something else. So in your case you would do something like this:

@Test
public void test1() {
    List<Person> names = new ArrayList<>();
    names.add(new Person("Bob"));
    names.add(new Person("i"));

    assertThat(names, hasItem(name(equalTo("Winkleburger"))));
}

private FeatureMatcher<Person, String> name(Matcher<String> matcher) {
    return new FeatureMatcher<Person, String>(matcher, "name", "name") {
        @Override
        protected String featureValueOf(Person actual) {
            return actual.name();
        }
    };
}

The benefit you'll get with this over a custom matcher is that it's fully reusable and composable with other matchers as all it does is the data-extraction then defers to whatever other matcher you want. You're also going to get appropriate diagnostics, e.g. in the above example you will if you change the assert to a value that's not present you will receive:

java.lang.AssertionError: 
    Expected: a collection containing name "Batman"
    but: name was "Bob", name was "Winkleburger"
like image 194
tddmonkey Avatar answered Oct 29 '22 01:10

tddmonkey


You can write one yourself:

public class HasName extends TypeSafeMatcher<MyClass> {
    private String expectedName;

    private HasName(String expectedName) {
        this.expectedName = expectedName;
    }

    @Override
    public boolean matchesSafely(MyClass obj) {
        return expectedName.equals(obj.name());
    }

    @Factory
    public static Matcher<MyClass> hasName(String expectedName) {
        return new HasName(expectedName);
    }
}

Where MyClass is a class or interface which defines the name() method.

like image 35
Tonio Avatar answered Oct 29 '22 00:10

Tonio