Java Code Examples for org.mockito.stubbing.Answer#answer()

The following examples show how to use org.mockito.stubbing.Answer#answer() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public Object answer(InvocationOnMock invocation) throws Throwable {
    //see ThreadsShareGenerouslyStubbedMockTest
    Answer a;
    synchronized(answers) {
        a = answers.size() == 1 ? answers.peek() : answers.poll();
    }
    return a.answer(invocation);
}
 
Example 2
@Test
public void shouldReturnAnObjectThatFailsOnAnyMethodInvocationForNonPrimitives() throws Throwable {
    Answer<Object> answer = new ReturnsSmartNulls();

    Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "get"));

    try {
        smartNull.get();
        fail();
    } catch (SmartNullPointerException expected) {}
}
 
Example 3
@Test
public void shouldReturnAnObjectThatAllowsObjectMethods() throws Throwable {
    Answer<Object> answer = new ReturnsSmartNulls();

    Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "get"));

    assertContains("SmartNull returned by", smartNull + "");
    assertContains("foo.get()", smartNull + "");
}
 
Example 4
@Test
public void shouldPrintTheParametersWhenCallingAMethodWithArgs() throws Throwable {
	Answer<Object> answer = new ReturnsSmartNulls();

	Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "withArgs", "oompa", "lumpa"));

    assertContains("foo.withArgs", smartNull + "");
    assertContains("oompa", smartNull + "");
    assertContains("lumpa", smartNull + "");
}
 
Example 5
@Test
public void shouldPrintTheParametersOnSmartNullPointerExceptionMessage() throws Throwable {
   	Answer<Object> answer = new ReturnsSmartNulls();

       Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "withArgs", "oompa", "lumpa"));

       try {
           smartNull.get();
           fail();
       } catch (SmartNullPointerException e) {
       	assertContains("oompa", e.getMessage());
       	assertContains("lumpa", e.getMessage());
       }
}
 
Example 6
public Object answer(InvocationOnMock invocation) throws Throwable {
    //see ThreadsShareGenerouslyStubbedMockTest
    Answer a;
    synchronized(answers) {
        a = answers.size() == 1 ? answers.peek() : answers.poll();
    }
    return a.answer(invocation);
}
 
Example 7
@Test
public void should_return_an_object_that_fails_on_any_method_invocation_for_non_primitives() throws Throwable {
    Answer<Object> answer = new ReturnsSmartNulls();

    Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "get"));

    try {
        smartNull.get();
        fail();
    } catch (SmartNullPointerException expected) {}
}
 
Example 8
@Test
public void should_return_an_object_that_allows_object_methods() throws Throwable {
    Answer<Object> answer = new ReturnsSmartNulls();

    Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "get"));

    assertContains("SmartNull returned by", smartNull + "");
    assertContains("foo.get()", smartNull + "");
}
 
Example 9
@Test
public void should_print_the_parameters_when_calling_a_method_with_args() throws Throwable {
	Answer<Object> answer = new ReturnsSmartNulls();

	Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "withArgs", "oompa", "lumpa"));

    assertContains("foo.withArgs", smartNull + "");
    assertContains("oompa", smartNull + "");
    assertContains("lumpa", smartNull + "");
}
 
Example 10
@Test
public void should_print_the_parameters_on_SmartNullPointerException_message() throws Throwable {
   	Answer<Object> answer = new ReturnsSmartNulls();

       Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "withArgs", "oompa", "lumpa"));

       try {
           smartNull.get();
           fail();
       } catch (SmartNullPointerException e) {
       	assertContains("oompa", e.getMessage());
       	assertContains("lumpa", e.getMessage());
       }
}
 
Example 11
@Override
public T answer(InvocationOnMock invocation) throws Throwable{
    Assert.assertTrue("Called too many times!",count<maxCount);
    Answer<T> stageAnswer = stageMap.get(count);
    Assert.assertNotNull("No Stage answer found!",stageAnswer);
    T ret = stageAnswer.answer(invocation);
    count++;
    return ret;
}