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

org.mockito.internal.stubbing.BaseStubbing Maven / Gradle / Ivy

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

import org.mockito.internal.stubbing.answers.CallsRealMethods;
import org.mockito.internal.stubbing.answers.Returns;
import org.mockito.internal.stubbing.answers.ThrowsException;
import org.mockito.internal.stubbing.answers.ThrowsExceptionClass;
import org.mockito.stubbing.DeprecatedOngoingStubbing;
import org.mockito.stubbing.OngoingStubbing;

public abstract class BaseStubbing implements OngoingStubbing, DeprecatedOngoingStubbing {
    public OngoingStubbing thenReturn(T value) {
        return thenAnswer(new Returns(value));
    }

    public OngoingStubbing thenReturn(T value, T... values) {
        OngoingStubbing stubbing = thenReturn(value);            
        if (values == null) {
            return stubbing.thenReturn(null);
        }
        for (T v: values) {
            stubbing = stubbing.thenReturn(v);
        }
        return stubbing;
    }

    private OngoingStubbing thenThrow(Throwable throwable) {
        return thenAnswer(new ThrowsException(throwable));
    }

    public OngoingStubbing thenThrow(Throwable... throwables) {
        if (throwables == null) {
            thenThrow((Throwable) null);
        }
        OngoingStubbing stubbing = null;
        for (Throwable t: throwables) {
            if (stubbing == null) {
                stubbing = thenThrow(t);                    
            } else {
                stubbing = stubbing.thenThrow(t);
            }
        }
        return stubbing;
    }        

    private OngoingStubbing thenThrow(Class throwableClass) {
        return thenAnswer(new ThrowsExceptionClass(throwableClass));
    }

    public OngoingStubbing thenThrow(Class... throwableClasses) {
        if (throwableClasses == null) {
            thenThrow((Throwable) null);
        }
        OngoingStubbing stubbing = null;
        for (Class t: throwableClasses) {
            if (stubbing == null) {
                stubbing = thenThrow(t);
            } else {
                stubbing = stubbing.thenThrow(t);
            }
        }
        return stubbing;
    }

    public OngoingStubbing thenCallRealMethod() {
        return thenAnswer(new CallsRealMethods());
    }

    public DeprecatedOngoingStubbing toReturn(T value) {
        return toAnswer(new Returns(value));
    }

    public DeprecatedOngoingStubbing toThrow(Throwable throwable) {
        return toAnswer(new ThrowsException(throwable));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy