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

com.fitbur.mockito.internal.stubbing.answers.AnswersValidator Maven / Gradle / Ivy

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

import static com.fitbur.mockito.exceptions.Reporter.cannotCallAbstractRealMethod;
import static com.fitbur.mockito.exceptions.Reporter.cannotStubVoidMethodWithAReturnValue;
import static com.fitbur.mockito.exceptions.Reporter.cannotStubWithNullThrowable;
import static com.fitbur.mockito.exceptions.Reporter.checkedExceptionInvalid;
import static com.fitbur.mockito.exceptions.Reporter.onlyVoidMethodsCanBeSetToDoNothing;
import static com.fitbur.mockito.exceptions.Reporter.wrongTypeOfArgumentToReturn;
import static com.fitbur.mockito.exceptions.Reporter.wrongTypeOfReturnValue;
import static com.fitbur.mockito.exceptions.Reporter.wrongTypeReturnedByDefaultAnswer;

import com.fitbur.mockito.invocation.Invocation;
import com.fitbur.mockito.stubbing.Answer;

public class AnswersValidator {


    public void validate(Answer answer, Invocation invocation) {
        MethodInfo methodInfo = new MethodInfo(invocation);
        if (answer instanceof ThrowsException) {
            validateException((ThrowsException) answer, methodInfo);
        }

        if (answer instanceof Returns) {
            validateReturnValue((Returns) answer, methodInfo);
        }

        if (answer instanceof DoesNothing) {
            validateDoNothing((DoesNothing) answer, methodInfo);
        }

        if (answer instanceof CallsRealMethods) {
            validateMockingConcreteClass((CallsRealMethods) answer, methodInfo);
        }

        if (answer instanceof ReturnsArgumentAt) {
            ReturnsArgumentAt returnsArgumentAt = (ReturnsArgumentAt) answer;
            validateReturnArgIdentity(returnsArgumentAt, invocation);
        }
    }

    private void validateReturnArgIdentity(ReturnsArgumentAt returnsArgumentAt, Invocation invocation) {
        returnsArgumentAt.validateIndexWithinInvocationRange(invocation);

        MethodInfo methodInfo = new MethodInfo(invocation);
        if (!methodInfo.isValidReturnType(returnsArgumentAt.returnedTypeOnSignature(invocation))) {
            throw wrongTypeOfArgumentToReturn(invocation, methodInfo.printMethodReturnType(),
                    returnsArgumentAt.returnedTypeOnSignature(invocation),
                    returnsArgumentAt.wantedArgumentPosition());
        }

    }

    private void validateMockingConcreteClass(CallsRealMethods answer, MethodInfo methodInfo) {
        if (methodInfo.isAbstract()) {
            throw cannotCallAbstractRealMethod();
        }
    }

    private void validateDoNothing(DoesNothing answer, MethodInfo methodInfo) {
        if (!methodInfo.isVoid()) {
            throw onlyVoidMethodsCanBeSetToDoNothing();
        }
    }

    private void validateReturnValue(Returns answer, MethodInfo methodInfo) {
        if (methodInfo.isVoid()) {
            throw cannotStubVoidMethodWithAReturnValue(methodInfo.getMethodName());
        }

        if (answer.returnsNull() && methodInfo.returnsPrimitive()) {
            throw wrongTypeOfReturnValue(methodInfo.printMethodReturnType(), "null", methodInfo.getMethodName());
        }

        if (!answer.returnsNull() && !methodInfo.isValidReturnType(answer.getReturnType())) {
            throw wrongTypeOfReturnValue(methodInfo.printMethodReturnType(), answer.printReturnType(), methodInfo.getMethodName());
        }
    }

    private void validateException(ThrowsException answer, MethodInfo methodInfo) {
        Throwable throwable = answer.getThrowable();
        if (throwable == null) {
            throw cannotStubWithNullThrowable();
        }

        if (throwable instanceof RuntimeException || throwable instanceof Error) {
            return;
        }

        if (!methodInfo.isValidException(throwable)) {
            throw checkedExceptionInvalid(throwable);
        }
    }

    public void validateDefaultAnswerReturnedValue(Invocation invocation, Object returnedValue) {
        MethodInfo methodInfo = new MethodInfo(invocation);
        if (returnedValue != null && !methodInfo.isValidReturnType(returnedValue.getClass())) {
            throw wrongTypeReturnedByDefaultAnswer(
                    invocation.getMock(),
                    methodInfo.printMethodReturnType(),
                    returnedValue.getClass().getSimpleName(),
                    methodInfo.getMethodName());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy