GithubHelp home page GithubHelp logo

redfin / contractual Goto Github PK

View Code? Open in Web Editor NEW
1.0 28.0 1.0 27 KB

Contractual is a set of Java interfaces that define generic test contracts. A JUnit 5 test class can then implement one or more of these test interfaces to inherit test cases that verify that their contract is fulfilled by the class under test.

License: Apache License 2.0

Java 100.00%

contractual's Introduction

Build Status License

Contractual

Overview

Contractual is a simple Java library of interfaces with default @Test methods for use with JUnit 5. This allows for generic tests to be written and re-used for every class that makes sense for that contract. It can also improve the writing of new classes in the first place. For instance, if you write a new class that overrides the equals method but forget to override the hashCode method, the EqualsContract interface, when applied to the new class's unit test class will fail.

Installation

<dependency>
    <groupId>com.redfin</groupId>
    <artifactId>contractual</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>

Example usage

Say you write a new class Foo that contains a String value. Two Foo instances should be equal only if they both contain the same String. To properly override the equals method you have to make sure that the equals method contract (defined in the Object class) is maintained. You also need to make sure that you override the hashCode method. An example implementation would be:

public class Foo {

    private final String s;

    public Foo(String s) {
        this.s = Objects.requireNonNull(s);
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof Foo && s.equals(((Foo)obj).s);
    }

    @Override
    public int hashCode() {
        return s.hashCode();
    }

    // other code specific to Foo
}

When it comes time to write the unit tests for Foo (say in a test class called FooTest) then there would normally be repeated code for each class that overrides the equals method. Contracts, along with JUnit 5, can spare you from repeated that test code (and possibly missing cases). The FooTest could just implement the interface, implement any abstract methods, and then it will inherit the test methods defined in the contract.

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;

@DisplayName("A Foo")
class FooTest implements EqualsContract<Foo> {

    private static final String EQUAL = "hello";
    private static final String NON_EQUAL = "world";

    @Nested
    @DisplayName("satisfies the EqualsContract as")
    class EqualityTest implements EqualsContract<Foo> {

        @Override
        public Foo getInstance() {
            return new Foo(EQUAL);
        }

        @Override
        public Supplier<Foo> getEqualInstanceSupplier() {
            return () -> new Foo(EQUAL);
        }

        @Override
        public Foo getNonEqualInstance() {
            return new Foo(NON_EQUAL);
        }
    }

    // any unit tests specific to Foo
}

contractual's People

Contributors

littleclay avatar

Stargazers

 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

Forkers

isabella232

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.