DefaultAnswerValidatorTest.java revision 2637d96c202372854a7c71466ddcc6e90fc4fc53
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.stubbing.answers;
6
7import org.junit.Test;
8import org.mockito.exceptions.misusing.WrongTypeOfReturnValue;
9import org.mockito.internal.invocation.InvocationBuilder;
10
11import static org.assertj.core.api.Assertions.assertThat;
12import static org.assertj.core.api.Assertions.fail;
13
14public class DefaultAnswerValidatorTest {
15
16    @Test
17    public void should_fail_if_returned_value_of_answer_is_incompatible_with_return_type() throws Throwable {
18        // given
19        class AWrongType {
20        }
21        try {
22            // when
23            DefaultAnswerValidator.validateReturnValueFor(new InvocationBuilder().method("toString").toInvocation(),
24                                                          new AWrongType());
25            fail("expected validation to fail");
26        } catch (WrongTypeOfReturnValue e) {
27            // then
28            assertThat(e.getMessage())
29                    .containsIgnoringCase("Default answer returned a result with the wrong type")
30                    .containsIgnoringCase("AWrongType cannot be returned by toString()")
31                    .containsIgnoringCase("toString() should return String");
32        }
33    }
34
35    @Test
36    public void should_not_fail_if_returned_value_of_answer_is_null() throws Throwable {
37        DefaultAnswerValidator.validateReturnValueFor(new InvocationBuilder().method("toString").toInvocation(),
38                                                      null);
39    }
40}
41