com.roscopeco.moxy.api.MoxyMultiVerifier Maven / Gradle / Ivy
/*
* Moxy - Lean-and-mean mocking framework for Java with a fluent API.
*
* Copyright 2018 Ross Bamford
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.roscopeco.moxy.api;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.MultipleFailuresError;
/**
* Implementations of this interface allow multiple mocks invocations to be
* verified after use. They are returned by the
* {@link MoxyEngine#assertMocks(com.roscopeco.moxy.api.InvocationRunnable)}
* method. For example:
*
*
* assertMocks(() -> {
* mock1.method1(any());
* mock1.method2(anyInt(), eq("Hello"));
*
* mock2.method1(anyBoolean());
* }).wereAllCalled();
*
*
* Individual engines will usually provide their own implementation
* of this interface, as verifying will require internal knowledge
* of the engine's mocking strategy.
*
* @author Ross Bamford <roscopeco AT gmail DOT com>
* @since 1.0
*/
public interface MoxyMultiVerifier {
/**
* Verify that none of the mock methods were called at all.
*
* Throws {@link AssertionFailedError} if a single invocation was
* matched, or {@link MultipleFailuresError} if multiple calls were
* matched. In the latter case, the individual calls that matched
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
*/
public void wereNotCalled();
/**
* Verify that all the mock methods were called at least once,
* with the arguments specified in the call.
*
* Note the subtle difference from {@link #wereAllCalledExactly(int)}, which
* expects an exact number of calls.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @return this
.
* @since 1.0
*/
public MoxyMultiVerifier wereAllCalled();
/**
* Verify that all the mock methods were called exactly expectedTimes
* with the arguments specified in the call.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
* @param expectedTimes The expected number of invocations for each method.
* @return this
.
*/
public MoxyMultiVerifier wereAllCalledExactly(int expectedTimes);
/**
* Verify that all the mock methods were called exactly once
* with the arguments specified in the call.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
* @return this
.
*/
public MoxyMultiVerifier wereAllCalledOnce();
/**
* Verify that all the mock methods were called exactly twice
* with the arguments specified in the call.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
* @return this
.
*/
public MoxyMultiVerifier wereAllCalledTwice();
/**
* Verify that all the mock methods were called at least expectedTimes
* with the arguments specified in the call.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
* @param expectedTimes The expected number of invocations.
* @return this
.
*/
public MoxyMultiVerifier wereAllCalledAtLeast(int expectedTimes);
/**
* Verify that all the mock methods were called at least twice
* with the arguments specified in the call.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
* @return this
.
*/
public MoxyMultiVerifier wereAllCalledAtLeastTwice();
/**
* Verify that all the mock methods were called at most expectedTimes
* with the arguments specified in the call.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
* @param expectedTimes The expected number of invocations.
* @return this
.
*/
public MoxyMultiVerifier wereAllCalledAtMost(int expectedTimes);
/**
* Verify that all the mock methods were called at most twice
* with the arguments specified in the call.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
* @return this
.
*/
public MoxyMultiVerifier wereAllCalledAtMostTwice();
/**
* Verifies that the mock methods were called (with matching
* arguments) in the order they were called within the lambda
* passed to assertMocks.
*
* This method is tolerant of other mock invocations in between
* the matched calls.
*
* Note that this does not directly verify
* that all methods were called (although it does have that side-effect).
* You may find it more readable (and will give better failure messages)
* to chain this with one of the {@link #wereAllCalled()} family
* of methods.
*
* Throws {@link AssertionFailedError} if the methods were not called
* in the order given.
*
* @since 1.0
*/
public void inThatOrder();
/**
* Verifies that the mock methods were called (with matching
* arguments) in the order they were called within the lambda
* passed to assertMocks.
*
* This method is not tolerant of other mock invocations in between
* the matched calls - it requires them to have been exclusively
* called, in the given order.
*
* Note that this does not directly verify
* that all methods were called (although it does have that side-effect).
* You may find it more readable (and will give better failure messages)
* to chain this with one of the {@link #wereAllCalled()} family
* of methods.
*
* Throws {@link AssertionFailedError} if the methods were not called
* in the order given.
*
* @since 1.0
*/
public void exclusivelyInThatOrder();
/**
* Verifies that the mock methods were called (with matching
* arguments) in any order. In the default implementation, this is
* a synonym for {@link #wereAllCalled()}.
*
* This method is tolerant of other mock invocations in between
* the matched calls.
*
* Throws {@link AssertionFailedError} if a single invocation wasn't
* matched, or {@link MultipleFailuresError} if multiple calls weren't
* matched. In the latter case, the individual calls that didn't match
* will each have a nested {@link AssertionFailedError}.
*
* @since 1.0
*/
public void inAnyOrder();
}