• 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 org.junit.Before;
9 import org.junit.Test;
10 import org.mockito.exceptions.base.MockitoException;
11 import org.mockito.internal.creation.MockSettingsImpl;
12 import org.mockito.internal.invocation.InvocationBuilder;
13 import org.mockito.internal.invocation.InvocationMatcher;
14 import org.mockito.internal.progress.MockingProgress;
15 import org.mockito.internal.stubbing.answers.Returns;
16 import org.mockito.internal.stubbing.answers.ThrowsException;
17 import org.mockito.invocation.Invocation;
18 import org.mockitoutil.TestBase;
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22 import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;
23 
24 public class InvocationContainerImplStubbingTest extends TestBase {
25 
26     private InvocationContainerImpl invocationContainerImpl;
27     private InvocationContainerImpl invocationContainerImplStubOnly;
28     private MockingProgress state;
29     private Invocation simpleMethod;
30 
31     @Before
setup()32     public void setup() {
33         state = mockingProgress();
34 
35         invocationContainerImpl = new InvocationContainerImpl(new MockSettingsImpl());
36         invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationBuilder().toInvocationMatcher());
37 
38         invocationContainerImplStubOnly =
39           new InvocationContainerImpl( new MockSettingsImpl().stubOnly());
40         invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationBuilder().toInvocationMatcher());
41 
42         simpleMethod = new InvocationBuilder().simpleMethod().toInvocation();
43     }
44 
45     @Test
should_finish_stubbing_when_wrong_throwable_is_set()46     public void should_finish_stubbing_when_wrong_throwable_is_set() throws Exception {
47         state.stubbingStarted();
48         try {
49             invocationContainerImpl.addAnswer(new ThrowsException(new Exception()), null);
50             fail();
51         } catch (MockitoException e) {
52             state.validateState();
53         }
54     }
55 
56     @Test
should_finish_stubbing_on_adding_return_value()57     public void should_finish_stubbing_on_adding_return_value() throws Exception {
58         state.stubbingStarted();
59         invocationContainerImpl.addAnswer(new Returns("test"), null);
60         state.validateState();
61     }
62 
63     @Test
should_get_results_for_methods()64     public void should_get_results_for_methods() throws Throwable {
65         invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
66         invocationContainerImpl.addAnswer(new Returns("simpleMethod"), null);
67 
68         Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
69         invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
70         invocationContainerImpl.addAnswer(new ThrowsException(new MyException()), null);
71 
72         assertEquals("simpleMethod", invocationContainerImpl.answerTo(simpleMethod));
73 
74         try {
75             invocationContainerImpl.answerTo(differentMethod);
76             fail();
77         } catch (MyException e) {}
78     }
79 
80     @Test
should_get_results_for_methods_stub_only()81     public void should_get_results_for_methods_stub_only() throws Throwable {
82         invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
83         invocationContainerImplStubOnly.addAnswer(new Returns("simpleMethod"), null);
84 
85         Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
86         invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
87         invocationContainerImplStubOnly.addAnswer(new ThrowsException(new MyException()), null);
88 
89         assertEquals("simpleMethod", invocationContainerImplStubOnly.answerTo(simpleMethod));
90 
91         try {
92             invocationContainerImplStubOnly.answerTo(differentMethod);
93             fail();
94         } catch (MyException e) {}
95     }
96 
97     @Test
should_validate_throwable()98     public void should_validate_throwable() throws Throwable {
99         try {
100             invocationContainerImpl.addAnswer(new ThrowsException(null), null);
101             fail();
102         } catch (MockitoException e) {}
103     }
104 
105     @SuppressWarnings("serial")
106     class MyException extends RuntimeException {}
107 }
108