Coder Social home page Coder Social logo

matchers's Introduction

Hamcrest 1.3 Utility Matchers

Build Status Coverage Status

Usage

The static factory methods for all matchers are generated to com.nitorcreations.Matchers class for easy access.

Contents

The package contains the following matchers:

  • CollectionMatchers
  • containsElements(Collection<E>) - matches that the target collection contains exactly the same items in exactly the same order
  • containsElementsInAnyOrder(Collection<E>) - matches that the target collection contains exactly the same items in any order
  • hasItemsOf(Collection<T> items) - matches that the target collection contains all of the items in the given collection. The collection may also contain other items.
  • hasNoDuplicates(Class<T>) - matches that the target collection contains no duplicates. The class parameter is to ensure generics work without unchecked casts.
  • emptyList() - matches empty lists
  • MapMatchers
  • emptyMap() - matches empty maps
  • HasItemAtIndex
  • hasFirstItem(Matcher<T> item)
  • hasLastItem(Matcher<T> item)
  • hasItemAtIndex(Matcher<T> item)
  • IteratorMatcher
  • iteratorThat(Iterable<T>)- wraps Iterable<T> matchers to work with Iterator<T>
  • ReflectionEqualsMatcher
  • reflectEquals(T target, String... excludedFields) - matches reflectively (shallow) that all fields except excludedFields are equal
  • SerializableMatcher
  • serializable() - matches that the given object can be serialized and deserialized
  • StringMatchers
  • containsIgnoreCase(String)
  • FieldMatchers
  • hasField(String fieldName) object has field with name fieldName
  • hasField(String fieldName, Matcher<?> valueMatcher) object has a field with name fieldName and its value matches the valueMatcher

Maven

Available in Maven central repository. Add this to your pom.xml

<dependency>
  <groupId>com.nitorcreations</groupId>
  <artifactId>matchers</artifactId>
  <version>1.3</version>
</dependency>

Contributing

New matchers are welcomed as pull requests, as long as they are properly unit tested.

If you add new matcher classes, be sure to add @Factory annotations to the static factory methods, and remember to add the new class to the matchers.xml file. This will ensure that the factory methods will be accessible in the common com.nitorcreations.Matchers class.

Example:

public final class HasItemAtIndex<X> extends TypeSafeDiagnosingMatcher<Iterable<X>> {
    @Factory
    public static <T> Matcher<Iterable<T>> hasFirstItem(Matcher<T> item) {
        return new HasItemAtIndex<T>(0, item);
    }

And the corresponding row in matchers.xml:

<matchers>
    ...
    <factory class="com.nitorcreations.matchers.HasItemAtIndex"/>
    ...
</matchers>

matchers's People

Contributors

rjokelai avatar vertti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

zeroone3010

matchers's Issues

FieldMatcher is unable to compare private field values

The FieldMatcher, which is supposed to work like hasField(String fieldName, Matcher<?> valueMatcher) when comparing field values, is unable to do the comparing if the field is private: it internally gets an exception like java.lang.IllegalAccessException: Class com.nitorcreations.matchers.FieldMatcher can not access a member of class foo.bar.Baz with modifiers "private".

The FieldMatcher is able to find the private fields and assert their existence just fine when used without the value matcher. However, I need to assert the value too. Alternatively, I would need a way to assert the return value of the getter of the private field.

My use case is that I've got a collection of entities where only two fields are interesting and the rest should be ignored:

assertThat(all, containsInAnyOrder(
    allOf(hasField("foo", equalTo("1111")), hasField("bar", equalTo("444"))),
    allOf(hasField("foo", equalTo("2222")), hasField("bar", equalTo("555"))),
    allOf(hasField("foo", equalTo("3333")), hasField("bar", equalTo("666")))
));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.