Search code examples
javajunithamcrest

Does the hamcrest library for JUnit has an API to test public variables rather than properties


I am trying to test public variables having specific values. The problem is that I am testing public variables, not JavaBean.

Assert.assertThat(clientMapper.entries, Matchers.<ClientMapper.MapperEntry>hasItem(
    Matchers.allOf(
            Matchers.hasProperty("from", Matchers.is("/v1")),
            Matchers.hasProperty("to", Matchers.is("/v2"))
    )
));

And the class I am testing is

public static class MapperEntry {
    public String from;
    public String to;
}

The test fails because it does not find a JavaBean property from and to, which is understandable, but how can I test public fields?

UPDATE: I am trying to find solution for java 7


Solution

  • NitorCreations' matchers library has matchers for fields:

    Assert.assertThat(clientMapper.entries, Matchers<ClientMapper.MapperEntry>hasItem(
      Matchers.allOf(
            com.nitorcreations.Matchers.hasField("from", Matchers.is("/v1")),
            com.nitorcreations.Matchers.hasField("to", Matchers.is("/v2"))
    )));