All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lpandzic.junit.bdd.When Maven / Gradle / Ivy

package com.github.lpandzic.junit.bdd;

import java.util.Optional;

/**
 * Defines behavior that is being tested.
 *
 * @author Lovro Pandzic
 */
public final class When {

    private final Bdd bdd;

    public When(Bdd bdd) {

        this.bdd = bdd;
    }

    /**
     * Used for specifying behavior that should throw an exception.
     *
     * @param throwableSupplier supplier or throwable
     * @param                type of throwable
     *
     * @return new {@link Then.Throws}
     */
    public  Then.Throws when(ThrowableSupplier throwableSupplier) {

        try {
            throwableSupplier.get();
        } catch (Throwable t) {
            bdd.putThrownException(t);
        }

        return new Then.Throws<>(bdd);
    }

    /**
     * Used for specifying behavior that should return a value.
     *
     * @param value returned by the specified behavior
     * @param    type of {@code value}
     *
     * @return new {@link Then.Returns}
     */
    public  Then.Returns when(T value) {

        return new Then.Returns<>(Optional.ofNullable(value));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy