ReturnsTest.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.base.MockitoException;
9import org.mockito.internal.invocation.InvocationBuilder;
10
11import static java.lang.Boolean.TRUE;
12import static org.assertj.core.api.Assertions.assertThat;
13
14public class ReturnsTest {
15    @Test
16    public void should_return_value() throws Throwable {
17        assertThat(new Returns("value").answer(new InvocationBuilder().method("oneArg").arg("A").toInvocation())).isEqualTo("value");
18    }
19
20    @Test(expected = MockitoException.class)
21    public void should_fail_when_return_Value_is_set_for_void_method() throws Throwable {
22        new Returns("one").validateFor(new InvocationBuilder().method("voidMethod").toInvocation());
23    }
24
25    @Test
26    public void should_allow_correct_type_of_return_value() throws Throwable {
27        new Returns("one").validateFor(new InvocationBuilder().simpleMethod().toInvocation());
28        new Returns(false).validateFor(new InvocationBuilder().method("booleanReturningMethod").toInvocation());
29        new Returns(TRUE).validateFor(new InvocationBuilder().method("booleanObjectReturningMethod").toInvocation());
30        new Returns(1).validateFor(new InvocationBuilder().method("integerReturningMethod").toInvocation());
31        new Returns(1L).validateFor(new InvocationBuilder().method("longReturningMethod").toInvocation());
32        new Returns(1L).validateFor(new InvocationBuilder().method("longObjectReturningMethod").toInvocation());
33        new Returns(null).validateFor(new InvocationBuilder().method("objectReturningMethodNoArgs").toInvocation());
34        new Returns(1).validateFor(new InvocationBuilder().method("objectReturningMethodNoArgs").toInvocation());
35    }
36
37    @Test(expected = MockitoException.class)
38    public void should_fail_on_return_type_mismatch() throws Throwable {
39        new Returns("String").validateFor(new InvocationBuilder().method("booleanReturningMethod").toInvocation());
40    }
41
42    @Test(expected = MockitoException.class)
43    public void should_fail_on_wrong_primitive() throws Throwable {
44        new Returns(1).validateFor(new InvocationBuilder().method("doubleReturningMethod").toInvocation());
45    }
46
47    @Test(expected = MockitoException.class)
48    public void should_fail_on_null_with_primitive() throws Throwable {
49        new Returns(null).validateFor(new InvocationBuilder().method("booleanReturningMethod").toInvocation());
50    }
51}
52