/** * Returns the parameter of an invocation at the given position. * * <p> * This additional answer could be used at stub time using the * <code>then|do|will{@link org.mockito.stubbing.Answer}</code> methods. For example : * </p> * * <pre class="code"><code class="java">given(person.remember(dream1, dream2, dream3, dream4)).will(returnsArgAt(3)); * daAnswer(returnsArgAt(3)).when(person).remember(dream1, dream2, dream3, dream4)</code></pre> * * @param <T> Return type of the invocation. * @return Answer that will return the second argument of the invocation. * * @since 1.9.5 */ public static <T> Answer<T> returnsArgAt(int position) { return (Answer<T>) new ReturnsArgumentAt(position); }
@Override public void validateFor(InvocationOnMock invocation) { int argumentPosition = inferWantedArgumentPosition(invocation); validateIndexWithinInvocationRange(invocation, argumentPosition); validateArgumentTypeCompatibility((Invocation) invocation, argumentPosition); }
public Object answer(InvocationOnMock invocation) throws Throwable { validateIndexWithinInvocationRange(invocation); return invocation.getArguments()[actualArgumentPosition(invocation)]; }
private int actualArgumentPosition(InvocationOnMock invocation) { return returningLastArg() ? lastArgumentIndexOf(invocation) : argumentIndexOf(invocation); }
@Override public Object answer(InvocationOnMock invocation) throws Throwable { int argumentPosition = inferWantedArgumentPosition(invocation); validateIndexWithinInvocationRange(invocation, argumentPosition); if (wantedArgIndexIsVarargAndSameTypeAsReturnType(invocation.getMethod(), argumentPosition)) { // answer raw vararg array argument return ((Invocation) invocation).getRawArguments()[argumentPosition]; } // answer expanded argument at wanted position return invocation.getArgument(argumentPosition); }
private void validateReturnArgIdentity(ReturnsArgumentAt returnsArgumentAt, Invocation invocation) { returnsArgumentAt.validateIndexWithinInvocationRange(invocation); MethodInfo methodInfo = new MethodInfo(invocation); if (!methodInfo.isValidReturnType(returnsArgumentAt.returnedTypeOnSignature(invocation))) { new Reporter().wrongTypeOfArgumentToReturn(invocation, methodInfo.printMethodReturnType(), returnsArgumentAt.returnedTypeOnSignature(invocation), returnsArgumentAt.wantedArgumentPosition()); } }
public void validateIndexWithinInvocationRange(InvocationOnMock invocation) { if (!argumentPositionInRange(invocation)) { new Reporter().invalidArgumentPositionRangeAtInvocationTime(invocation, returningLastArg(), wantedArgumentPosition); } }
private boolean argumentPositionInRange(InvocationOnMock invocation) { int actualArgumentPosition = actualArgumentPosition(invocation); if (actualArgumentPosition < 0) { return false; } if (!invocation.getMethod().isVarArgs()) { return invocation.getArguments().length > actualArgumentPosition; } // for all varargs accepts positive ranges return true; }
/** * Build the identity answer to return the argument at the given position in the argument array. * * @param wantedArgumentPosition The position of the argument identity to return in the invocation. * Using <code>-1</code> indicates the last argument. */ public ReturnsArgumentAt(int wantedArgumentPosition) { this.wantedArgumentPosition = checkWithinAllowedRange(wantedArgumentPosition); }
public Class returnedTypeOnSignature(InvocationOnMock invocation) { int actualArgumentPosition = actualArgumentPosition(invocation); if(!invocation.getMethod().isVarArgs()) { return invocation.getMethod().getParameterTypes()[actualArgumentPosition]; } Class<?>[] parameterTypes = invocation.getMethod().getParameterTypes(); int varargPosition = parameterTypes.length - 1; if(actualArgumentPosition < varargPosition) { return parameterTypes[actualArgumentPosition]; } else { return parameterTypes[varargPosition].getComponentType(); } } }
return (Answer<T>) new ReturnsArgumentAt(position);
return (Answer<T>) new ReturnsArgumentAt(0);
return (Answer<T>) new ReturnsArgumentAt(1);
return (Answer<T>) new ReturnsArgumentAt(ReturnsArgumentAt.LAST_ARGUMENT);
private static void mockMessagesManager() { MockMessagesManager messagesManager = mock(MockMessagesManager.class); Messages messages = mock(Messages.class); doAnswer(new ReturnsArgumentAt(0)).when(messages).get(anyString()); doReturn(messages).when(messagesManager).getMessagesInternal(anyString(), any(Locale.class)); ComponentsTestUtil.setInstance(MessagesManager.class, messagesManager); }
@Before public void setup() { when(interpreter.renderFlat(anyString())).thenAnswer(new ReturnsArgumentAt(0)); }
@Before public void setup() { when(interpreter.renderFlat(anyString())).thenAnswer(new ReturnsArgumentAt(0)); }
@Before public void setUp() throws Exception { // Init context session = new MockSession(RepositoryConstants.WEBSITE); ctx = new MockContext(); ctx.addSession(RepositoryConstants.WEBSITE, session); User user = mock(User.class); when(user.getName()).thenReturn(TEST_USER); ctx.setUser(user); MgnlContext.setInstance(ctx); // Mock scheduler schedulerModuleProvider = mock(Provider.class); SchedulerModule schedulerModule = mock(SchedulerModule.class); given(schedulerModuleProvider.get()).willReturn(schedulerModule); scheduler = mock(Scheduler.class); given(schedulerModule.getScheduler()).willReturn(scheduler); given(scheduler.getListenerManager()).willReturn(listenerManager); definition = new CommandActionDefinition(); i18n = mock(SimpleTranslator.class, new ReturnsArgumentAt(0)); item = new JcrNodeAdapter(session.getRootNode()); }
@Test public void executeAsynchronously() throws Exception { // GIVEN definition.setAsynchronous(true); definition.setCommand("qux"); JcrItemAdapter item = new JcrNodeAdapter(session.getNode("/parent/sub")); SimpleTranslator i18n = mock(SimpleTranslator.class, new ReturnsArgumentAt(0)); AbstractCommandAction<CommandActionDefinition> action = new AbstractCommandAction<>(definition, item, commandsManager, uiContext, i18n); // WHEN action.execute(); // THEN assertEquals("Stop Processing = false as it's invoke asynchronously", false, MgnlContext.getAttribute(AbstractCommandAction.COMMAND_RESULT, Context.LOCAL_SCOPE)); assertThat(action.getSuccessMessage(), equalTo(LONG_RUNNING_ACTION_NOTIFICATION)); }
appDescriptor, mock(SystemMonitor.class), mock(I18nizer.class, new ReturnsArgumentAt(0)), i18n); appInstanceControllerImpl.setAppComponentProvider(Components.getComponentProvider());