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

com.fitbur.mockito.internal.stubbing.StubberImpl 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;

import static com.fitbur.mockito.exceptions.Reporter.notAMockPassedToWhenMethod;
import static com.fitbur.mockito.exceptions.Reporter.nullPassedToWhenMethod;

import java.util.LinkedList;
import java.util.List;

import com.fitbur.mockito.internal.stubbing.answers.CallsRealMethods;
import com.fitbur.mockito.internal.stubbing.answers.DoesNothing;
import com.fitbur.mockito.internal.stubbing.answers.Returns;
import com.fitbur.mockito.internal.stubbing.answers.ThrowsException;
import com.fitbur.mockito.internal.stubbing.answers.ThrowsExceptionClass;
import com.fitbur.mockito.internal.util.MockUtil;
import com.fitbur.mockito.stubbing.Answer;
import com.fitbur.mockito.stubbing.Stubber;

@SuppressWarnings("unchecked")
public class StubberImpl implements Stubber {

    private final List> answers = new LinkedList>();

    public  T when(T mock) {
        MockUtil mockUtil = new MockUtil();
        
        if (mock == null) {
            throw nullPassedToWhenMethod();
        } 
        
		if (!mockUtil.isMock(mock)) {
			throw notAMockPassedToWhenMethod();
		}
        
        mockUtil.getMockHandler(mock).setAnswersForStubbing(answers);
        return mock;
    }

    public Stubber doReturn(Object toBeReturned) {
        return doReturnValues(toBeReturned);
    }

    public Stubber doReturn(Object toBeReturned, Object... nextToBeReturned) {
        return doReturnValues(toBeReturned).doReturnValues(nextToBeReturned);
    }

    private StubberImpl doReturnValues(Object... toBeReturned) {
        if(toBeReturned == null) {
            answers.add(new Returns(null));
            return this;
        }
        for (Object r : toBeReturned) {
            answers.add(new Returns(r));
        }
        return this;
    }

    public Stubber doThrow(Throwable... toBeThrown) {
        if(toBeThrown == null) {
            answers.add(new ThrowsException(null));
            return this;
        }
        for (Throwable throwable : toBeThrown) {
            answers.add(new ThrowsException(throwable));
        }
        return this;
    }

    public Stubber doThrow(Class toBeThrown) {
        return doThrowClasses(toBeThrown);
    }

    public Stubber doThrow(Class toBeThrown, Class... nextToBeThrown) {
        return doThrowClasses(toBeThrown).doThrowClasses(nextToBeThrown);
    }

    private StubberImpl doThrowClasses(Class... toBeThrown) {
        for (Class throwable: toBeThrown) {
            answers.add(new ThrowsExceptionClass(throwable));
        }
        return this;
    }

    public Stubber doNothing() {
        answers.add(new DoesNothing());
        return this;
    }

    public Stubber doAnswer(Answer answer) {
        answers.add(answer);
        return this;
    }

    public Stubber doCallRealMethod() {
        answers.add(new CallsRealMethods());
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy