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

org.mockito.internal.stubbing.answers.AnswerFunctionalInterfaces Maven / Gradle / Ivy

There is a newer version: 5.11.0
Show newest version
/*
 * Copyright (c) 2016 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito.internal.stubbing.answers;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Answer1;
import org.mockito.stubbing.Answer2;
import org.mockito.stubbing.Answer3;
import org.mockito.stubbing.Answer4;
import org.mockito.stubbing.Answer5;
import org.mockito.stubbing.Answer6;
import org.mockito.stubbing.VoidAnswer1;
import org.mockito.stubbing.VoidAnswer2;
import org.mockito.stubbing.VoidAnswer3;
import org.mockito.stubbing.VoidAnswer4;
import org.mockito.stubbing.VoidAnswer5;
import org.mockito.stubbing.VoidAnswer6;

/**
 * Functional interfaces to make it easy to implement answers in Java 8
 *
 * @since 2.1.0
 */
public class AnswerFunctionalInterfaces {
    /**
     * Hide constructor to avoid instantiation of class with only static methods
     */
    private AnswerFunctionalInterfaces() {}

    /**
     * Construct an answer from a two parameter answer interface
     * @param answer answer interface
     * @param  return type
     * @param  input parameter 1 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final Answer1 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public T answer(InvocationOnMock invocation) throws Throwable {
                return answer.answer((A) invocation.getArgument(0));
            }
        };
    }

    /**
     * Construct an answer from a two parameter answer interface
     * @param answer answer interface
     * @param  input parameter 1 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final VoidAnswer1 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public Void answer(InvocationOnMock invocation) throws Throwable {
                answer.answer((A) invocation.getArgument(0));
                return null;
            }
        };
    }

    /**
     * Construct an answer from a two parameter answer interface
     * @param answer answer interface
     * @param  return type
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final Answer2 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public T answer(InvocationOnMock invocation) throws Throwable {
                return answer.answer((A) invocation.getArgument(0), (B) invocation.getArgument(1));
            }
        };
    }

    /**
     * Construct an answer from a two parameter answer interface
     * @param answer answer interface
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final VoidAnswer2 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public Void answer(InvocationOnMock invocation) throws Throwable {
                answer.answer((A) invocation.getArgument(0), (B) invocation.getArgument(1));
                return null;
            }
        };
    }

    /**
     * Construct an answer from a three parameter answer interface
     * @param answer answer interface
     * @param  return type
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @param  input parameter 3 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final Answer3 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public T answer(InvocationOnMock invocation) throws Throwable {
                return answer.answer(
                        (A) invocation.getArgument(0),
                        (B) invocation.getArgument(1),
                        (C) invocation.getArgument(2));
            }
        };
    }

    /**
     * Construct an answer from a three parameter answer interface
     * @param answer answer interface
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @param  input parameter 3 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final VoidAnswer3 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public Void answer(InvocationOnMock invocation) throws Throwable {
                answer.answer(
                        (A) invocation.getArgument(0),
                        (B) invocation.getArgument(1),
                        (C) invocation.getArgument(2));
                return null;
            }
        };
    }

    /**
     * Construct an answer from a four parameter answer interface
     * @param answer answer interface
     * @param  return type
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @param  input parameter 3 type
     * @param  input parameter 4 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final Answer4 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public T answer(InvocationOnMock invocation) throws Throwable {
                return answer.answer(
                        (A) invocation.getArgument(0),
                        (B) invocation.getArgument(1),
                        (C) invocation.getArgument(2),
                        (D) invocation.getArgument(3));
            }
        };
    }

    /**
     * Construct an answer from a four parameter answer interface
     * @param answer answer interface
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @param  input parameter 3 type
     * @param  input parameter 4 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final VoidAnswer4 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public Void answer(InvocationOnMock invocation) throws Throwable {
                answer.answer(
                        (A) invocation.getArgument(0),
                        (B) invocation.getArgument(1),
                        (C) invocation.getArgument(2),
                        (D) invocation.getArgument(3));
                return null;
            }
        };
    }

    /**
     * Construct an answer from a five parameter answer interface
     * @param answer answer interface
     * @param  return type
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @param  input parameter 3 type
     * @param  input parameter 4 type
     * @param  input parameter 5 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final Answer5 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public T answer(InvocationOnMock invocation) throws Throwable {
                return answer.answer(
                        (A) invocation.getArgument(0),
                        (B) invocation.getArgument(1),
                        (C) invocation.getArgument(2),
                        (D) invocation.getArgument(3),
                        (E) invocation.getArgument(4));
            }
        };
    }

    /**
     * Construct an answer from a five parameter answer interface
     * @param answer answer interface
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @param  input parameter 3 type
     * @param  input parameter 4 type
     * @param  input parameter 5 type
     * @return a new answer object
     */
    public static  Answer toAnswer(final VoidAnswer5 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public Void answer(InvocationOnMock invocation) throws Throwable {
                answer.answer(
                        (A) invocation.getArgument(0),
                        (B) invocation.getArgument(1),
                        (C) invocation.getArgument(2),
                        (D) invocation.getArgument(3),
                        (E) invocation.getArgument(4));
                return null;
            }
        };
    }

    /**
     * Construct an answer from a six parameter answer interface
     *
     * @param answer answer interface
     * @param  return type
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @param  input parameter 3 type
     * @param  input parameter 4 type
     * @param  input parameter 5 type
     * @param  input parameter 6 type
     * @return a new answer object
     */
    public static  Answer toAnswer(
            final Answer6 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public T answer(InvocationOnMock invocation) throws Throwable {
                return answer.answer(
                        (A) invocation.getArgument(0),
                        (B) invocation.getArgument(1),
                        (C) invocation.getArgument(2),
                        (D) invocation.getArgument(3),
                        (E) invocation.getArgument(4),
                        (F) invocation.getArgument(5));
            }
        };
    }

    /**
     * Construct an answer from a five parameter answer interface
     *
     * @param answer answer interface
     * @param  input parameter 1 type
     * @param  input parameter 2 type
     * @param  input parameter 3 type
     * @param  input parameter 4 type
     * @param  input parameter 5 type
     * @param  input parameter 6 type
     * @return a new answer object
     */
    public static  Answer toAnswer(
            final VoidAnswer6 answer) {
        return new Answer() {
            @Override
            @SuppressWarnings("unchecked")
            public Void answer(InvocationOnMock invocation) throws Throwable {
                answer.answer(
                        (A) invocation.getArgument(0),
                        (B) invocation.getArgument(1),
                        (C) invocation.getArgument(2),
                        (D) invocation.getArgument(3),
                        (E) invocation.getArgument(4),
                        (F) invocation.getArgument(5));
                return null;
            }
        };
    }
}