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

org.mockito.stubbing.Answer Maven / Gradle / Ivy

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

import org.mockito.invocation.InvocationOnMock;

/**
 * Generic interface to be used for configuring mock's answer.
 * Answer specifies an action that is executed and a return value that is returned when you interact with the mock.
 * 

* Example of stubbing a mock with custom answer: * *


 * when(mock.someMethod(anyString())).thenAnswer(
 *     new Answer() {
 *         public Object answer(InvocationOnMock invocation) {
 *             Object[] args = invocation.getArguments();
 *             Object mock = invocation.getMock();
 *             return "called with arguments: " + Arrays.toString(args);
 *         }
 * });
 *
 * //Following prints "called with arguments: [foo]"
 * System.out.println(mock.someMethod("foo"));
 * 
* * @param the type to return. */ public interface Answer { /** * @param invocation the invocation on the mock. * * @return the value to be returned * * @throws Throwable the throwable to be thrown */ T answer(InvocationOnMock invocation) throws Throwable; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy