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

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

The 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 static org.mockito.internal.exceptions.Reporter.notAnException;
import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;
import static org.objenesis.ObjenesisHelper.newInstance;

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

public abstract class BaseStubbing implements OngoingStubbing {

    @Override
    public OngoingStubbing thenReturn(T value) {
        return thenAnswer(new Returns(value));
    }

    @Override
    public OngoingStubbing thenReturn(T value, T... values) {
        OngoingStubbing stubbing = thenReturn(value);
        if (values == null) {
            // TODO below does not seem right
            return stubbing.thenReturn(null);
        }
        for (T v : values) {
            stubbing = stubbing.thenReturn(v);
        }
        return stubbing;
    }

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

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

    @Override
    public OngoingStubbing thenThrow(Class throwableType) {
        if (throwableType == null) {
            mockingProgress().reset();
            throw notAnException();
        }
        return thenThrow(newInstance(throwableType));
    }

    @Override
    public OngoingStubbing thenThrow(Class toBeThrown, Class... nextToBeThrown) {
        if (nextToBeThrown == null) {
            thenThrow((Class) null);
        }
        OngoingStubbing stubbing = thenThrow(toBeThrown);
        for (Class t : nextToBeThrown) {
            stubbing = stubbing.thenThrow(t);
        }
        return stubbing;
    }

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






© 2015 - 2025 Weber Informatics LLC | Privacy Policy