• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockito.internal.stubbing;
7 
8 import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;
9 
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.mockito.exceptions.base.MockitoException;
13 import org.mockito.internal.creation.MockSettingsImpl;
14 import org.mockito.internal.invocation.InvocationBuilder;
15 import org.mockito.internal.invocation.InvocationMatcher;
16 import org.mockito.internal.progress.MockingProgress;
17 import org.mockito.internal.stubbing.answers.Returns;
18 import org.mockito.internal.stubbing.answers.ThrowsException;
19 import org.mockito.invocation.Invocation;
20 import org.mockitoutil.TestBase;
21 
22 import static junit.framework.TestCase.assertEquals;
23 import static junit.framework.TestCase.fail;
24 
25 public class InvocationContainerImplStubbingTest extends TestBase {
26 
27     private InvocationContainerImpl invocationContainerImpl;
28     private InvocationContainerImpl invocationContainerImplStubOnly;
29     private MockingProgress state;
30     private Invocation simpleMethod;
31 
32     @Before
setup()33     public void setup() {
34         state = mockingProgress();
35 
36         invocationContainerImpl = new InvocationContainerImpl(new MockSettingsImpl());
37         invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationBuilder().toInvocationMatcher());
38 
39         invocationContainerImplStubOnly =
40           new InvocationContainerImpl( new MockSettingsImpl().stubOnly());
41         invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationBuilder().toInvocationMatcher());
42 
43         simpleMethod = new InvocationBuilder().simpleMethod().toInvocation();
44     }
45 
46     @Test
should_finish_stubbing_when_wrong_throwable_is_set()47     public void should_finish_stubbing_when_wrong_throwable_is_set() throws Exception {
48         state.stubbingStarted();
49         try {
50             invocationContainerImpl.addAnswer(new ThrowsException(new Exception()));
51             fail();
52         } catch (MockitoException e) {
53             state.validateState();
54         }
55     }
56 
57     @Test
should_finish_stubbing_on_adding_return_value()58     public void should_finish_stubbing_on_adding_return_value() throws Exception {
59         state.stubbingStarted();
60         invocationContainerImpl.addAnswer(new Returns("test"));
61         state.validateState();
62     }
63 
64     @Test
should_get_results_for_methods()65     public void should_get_results_for_methods() throws Throwable {
66         invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
67         invocationContainerImpl.addAnswer(new Returns("simpleMethod"));
68 
69         Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
70         invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
71         invocationContainerImpl.addAnswer(new ThrowsException(new MyException()));
72 
73         assertEquals("simpleMethod", invocationContainerImpl.answerTo(simpleMethod));
74 
75         try {
76             invocationContainerImpl.answerTo(differentMethod);
77             fail();
78         } catch (MyException e) {}
79     }
80 
81     @Test
should_get_results_for_methods_stub_only()82     public void should_get_results_for_methods_stub_only() throws Throwable {
83         invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
84         invocationContainerImplStubOnly.addAnswer(new Returns("simpleMethod"));
85 
86         Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
87         invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
88         invocationContainerImplStubOnly.addAnswer(new ThrowsException(new MyException()));
89 
90         assertEquals("simpleMethod", invocationContainerImplStubOnly.answerTo(simpleMethod));
91 
92         try {
93             invocationContainerImplStubOnly.answerTo(differentMethod);
94             fail();
95         } catch (MyException e) {}
96     }
97 
98     @Test
should_add_throwable_for_void_method()99     public void should_add_throwable_for_void_method() throws Throwable {
100         invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new MyException()));
101         invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod));
102 
103         try {
104             invocationContainerImpl.answerTo(simpleMethod);
105             fail();
106         } catch (MyException e) {}
107     }
108 
109     @Test
should_validate_throwable_for_void_method()110     public void should_validate_throwable_for_void_method() throws Throwable {
111         invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new Exception()));
112 
113         try {
114             invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod));
115             fail();
116         } catch (MockitoException e) {}
117     }
118 
119     @Test
should_validate_throwable()120     public void should_validate_throwable() throws Throwable {
121         try {
122             invocationContainerImpl.addAnswer(new ThrowsException(null));
123             fail();
124         } catch (MockitoException e) {}
125     }
126 
127     @SuppressWarnings("serial")
128     class MyException extends RuntimeException {}
129 }
130