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

pl.hiber.testcase.statements.SubAnswer Maven / Gradle / Ivy

There is a newer version: 0.12
Show newest version
package pl.hiber.testcase.statements;

import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.io.Serializable;

/**
 * Created 2014 by 4ib3r
 * Custom mock answer, used for build list method for invoke in test case
 */
public final class SubAnswer implements Answer, Serializable {

    private FrameworkMethodAction parentMethod;

    public SubAnswer(FrameworkMethodAction parentMethod) {
        this.parentMethod = parentMethod;
    }

    /**
     * When method in testCase method is invoked this method add this method and arguments to invoke list.
     * */
	@Override
	public Object answer(InvocationOnMock invocation) throws Throwable {
        FrameworkMethodAction fma = new FrameworkMethodAction(invocation.getMethod());
        fma.setParameters(invocation.getArguments());
        if (!"final".equals(fma.getName()) && !"toString".equals(fma.getName()) && !"hash".equals(fma.getName())) {
            parentMethod.setChainMethod(fma);
            if (invocation.getMethod().getReturnType() != Void.TYPE) {
                return Mockito.mock(invocation.getMethod().getReturnType(), new SubAnswer(fma));
            }
        }
        return null;
	}
}