private Object deepStub(InvocationOnMock invocation, GenericMetadataSupport returnTypeGenericMetadata) throws Throwable { InvocationContainerImpl container = MockUtil.getInvocationContainer(invocation.getMock()); // matches invocation for verification // TODO why don't we do container.findAnswer here? for (Stubbing stubbing : container.getStubbingsDescending()) { if (container.getInvocationForStubbing().matches(stubbing.getInvocation())) { return stubbing.answer(invocation); } } // record deep stub answer StubbedInvocationMatcher stubbing = recordDeepStubAnswer( newDeepStubMock(returnTypeGenericMetadata, invocation.getMock()), container ); // deep stubbing creates a stubbing and immediately uses it // so the stubbing is actually used by the same invocation stubbing.markStubUsed(stubbing.getInvocation()); return stubbing.answer(invocation); }
/** * Adds new stubbed answer and returns the invocation matcher the answer was added to. */ public StubbedInvocationMatcher addAnswer(Answer answer, boolean isConsecutive, Strictness stubbingStrictness) { Invocation invocation = invocationForStubbing.getInvocation(); mockingProgress().stubbingCompleted(); if (answer instanceof ValidableAnswer) { ((ValidableAnswer) answer).validateFor(invocation); } synchronized (stubbed) { if (isConsecutive) { stubbed.getFirst().addAnswer(answer); } else { Strictness effectiveStrictness = stubbingStrictness != null ? stubbingStrictness : this.mockStrictness; stubbed.addFirst(new StubbedInvocationMatcher(answer, invocationForStubbing, effectiveStrictness)); } return stubbed.getFirst(); } }
public StubbedInvocationMatcher findAnswerFor(Invocation invocation) { synchronized (stubbed) { for (StubbedInvocationMatcher s : stubbed) { if (s.matches(invocation)) { s.markStubUsed(invocation); //TODO we should mark stubbed at the point of stubbing, not at the point where the stub is being used invocation.markStubbed(new StubInfoImpl(s)); return s; } } } return null; }
/** * Finds all unused stubs for given mocks * * @param mocks */ public List<Invocation> find(List<?> mocks) { List<Invocation> unused = new LinkedList<Invocation>(); for (Object mock : mocks) { InternalMockHandler<Object> handler = new MockUtil().getMockHandler(mock); List<StubbedInvocationMatcher> fromSingleMock = handler.getInvocationContainer().getStubbedInvocations(); for(StubbedInvocationMatcher s : fromSingleMock) { if (!s.wasUsed()) { unused.add(s.getInvocation()); } } } return unused; } }
stubbedInvocation.captureArgumentsFrom(invocation); return stubbedInvocation.answer(invocation); } else { Object ret = mockSettings.getDefaultAnswer().answer(invocation);
private Object getMock(InvocationOnMock invocation) throws Throwable { InternalMockHandler<Object> handler = new MockUtil().getMockHandler(invocation.getMock()); InvocationContainerImpl container = (InvocationContainerImpl) handler.getInvocationContainer(); // matches invocation for verification for (StubbedInvocationMatcher stubbedInvocationMatcher : container.getStubbedInvocations()) { if(container.getInvocationForStubbing().matches(stubbedInvocationMatcher.getInvocation())) { return stubbedInvocationMatcher.answer(invocation); } } // deep stub return recordDeepStubMock(invocation, container); }
Object answerTo(Invocation invocation) throws Throwable { return findAnswerFor(invocation).answer(invocation); }
StubbedInvocationMatcher subbedInvocation = invocationContainer .getStubbedInvocations().get(0); Invocation invocation = subbedInvocation.getInvocation(); Method method = invocation.getMethod(); Object[] rawArguments = invocation.getRawArguments();
stubbing.captureArgumentsFrom(invocation); return stubbing.answer(invocation); } finally {
/** * Finds all unused stubs for given mocks * * @param mocks */ public List<Invocation> find(List<?> mocks) { List<Invocation> unused = new LinkedList<Invocation>(); for (Object mock : mocks) { MockHandlerInterface<Object> handler = new MockUtil().getMockHandler(mock); List<StubbedInvocationMatcher> fromSingleMock = handler.getInvocationContainer().getStubbedInvocations(); for(StubbedInvocationMatcher s : fromSingleMock) { if (!s.wasUsed()) { unused.add(s.getInvocation()); } } } return unused; } }
Object answerTo(Invocation invocation) throws Throwable { return findAnswerFor(invocation).answer(invocation); }
public StubbedInvocationMatcher findAnswerFor(Invocation invocation) { synchronized (stubbed) { for (StubbedInvocationMatcher s : stubbed) { if (s.matches(invocation)) { s.markStubUsed(invocation); invocation.markStubbed(new StubInfoImpl(s)); return s; } } } return null; }
public void addAnswer(Answer answer, boolean isConsecutive) { Invocation invocation = invocationForStubbing.getInvocation(); mockingProgress.stubbingCompleted(invocation); AnswersValidator answersValidator = new AnswersValidator(); answersValidator.validate(answer, invocation); synchronized (stubbed) { if (isConsecutive) { stubbed.getFirst().addAnswer(answer); } else { stubbed.addFirst(new StubbedInvocationMatcher(invocationForStubbing, answer)); } } }
stubbedInvocation.captureArgumentsFrom(invocation); return stubbedInvocation.answer(invocation); } else { Object ret = mockSettings.getDefaultAnswer().answer(invocation);
Object answerTo(Invocation invocation) throws Throwable { return findAnswerFor(invocation).answer(invocation); }
public StubbedInvocationMatcher findAnswerFor(Invocation invocation) { synchronized (stubbed) { for (StubbedInvocationMatcher s : stubbed) { if (s.matches(invocation)) { s.markStubUsed(invocation); invocation.markStubbed(new StubInfo(s)); return s; } } } return null; }
public void addAnswer(Answer answer, boolean isConsecutive) { Invocation invocation = invocationForStubbing.getInvocation(); mockingProgress.stubbingCompleted(invocation); AnswersValidator answersValidator = new AnswersValidator(); answersValidator.validate(answer, invocation); synchronized (stubbed) { if (isConsecutive) { stubbed.getFirst().addAnswer(answer); } else { stubbed.addFirst(new StubbedInvocationMatcher(invocationForStubbing, answer)); } } }