Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
60   173   16   4,62
0   123   0,27   3,25
13     1,23  
4    
 
  AnswerTest       Line # 30 60 0% 16 3 95,9% 0.9589041
  AnswerTest.A       Line # 123 0 - 0 0 - -1.0
  AnswerTest.B       Line # 126 0 - 0 0 - -1.0
  AnswerTest.C       Line # 129 0 - 0 0 - -1.0
 
  (12)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock.tests2;
18   
19    import static org.easymock.EasyMock.*;
20    import static org.junit.Assert.*;
21   
22    import org.easymock.IAnswer;
23    import org.easymock.tests.IMethods;
24    import org.junit.Before;
25    import org.junit.Test;
26   
27    /**
28    * @author OFFIS, Tammo Freese
29    */
 
30    public class AnswerTest {
31   
32    private IMethods mock;
33   
 
34  12 toggle @Before
35    public void setUp() {
36  12 mock = createMock(IMethods.class);
37    }
38   
 
39  2 toggle @Test
40    public void answer() {
41  2 final IAnswer<Object> firstAnswer = new IAnswer<Object>() {
 
42  2 toggle public Object answer() {
43  2 assertArrayEquals(new Object[] { 1, "2", "3" }, getCurrentArguments());
44  2 return "Call answered";
45    }
46    };
47   
48  2 final IAnswer<Object> secondAnswer = new IAnswer<Object>() {
 
49  2 toggle public Object answer() {
50  2 assertArrayEquals(new Object[] { 1, "2", "3" }, getCurrentArguments());
51  2 throw new IllegalStateException("Call answered");
52    }
53    };
54   
55  2 expect(mock.threeArgumentMethod(1, "2", "3")).andAnswer(firstAnswer).andReturn("Second call")
56    .andAnswer(secondAnswer).andReturn("Fourth call");
57   
58  2 replay(mock);
59   
60  2 assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "3"));
61  2 assertEquals("Second call", mock.threeArgumentMethod(1, "2", "3"));
62  2 try {
63  2 mock.threeArgumentMethod(1, "2", "3");
64  0 fail();
65    } catch (final IllegalStateException expected) {
66  2 assertEquals("Call answered", expected.getMessage());
67    }
68  2 assertEquals("Fourth call", mock.threeArgumentMethod(1, "2", "3"));
69   
70  2 verify(mock);
71    }
72   
 
73  2 toggle @Test
74    public void stubAnswer() {
75  2 final IAnswer<Object> firstAnswer = new IAnswer<Object>() {
 
76  6 toggle public Object answer() {
77  6 assertArrayEquals(new Object[] { 1, "2", "3" }, getCurrentArguments());
78  6 return "Call answered";
79    }
80    };
81   
82  2 final IAnswer<Object> secondAnswer = new IAnswer<Object>() {
 
83  2 toggle public Object answer() {
84  2 assertArrayEquals(new Object[] { 1, "2", "4" }, getCurrentArguments());
85  2 return "Call answered";
86    }
87    };
88   
89  2 expect(mock.threeArgumentMethod(1, "2", "3")).andReturn(42).andStubAnswer(firstAnswer);
90  2 expect(mock.threeArgumentMethod(1, "2", "4")).andStubAnswer(secondAnswer);
91   
92  2 replay(mock);
93   
94  2 assertEquals(42, mock.threeArgumentMethod(1, "2", "3"));
95  2 assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "3"));
96  2 assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "4"));
97  2 assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "3"));
98  2 assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "3"));
99   
100  2 verify(mock);
101    }
102   
 
103  2 toggle @Test
104    public void nullAnswerNotAllowed() {
105  2 try {
106  2 expect(mock.threeArgumentMethod(1, "2", "3")).andAnswer(null);
107  0 fail();
108    } catch (final NullPointerException expected) {
109  2 assertEquals("answer object must not be null", expected.getMessage());
110    }
111    }
112   
 
113  2 toggle @Test
114    public void nullStubAnswerNotAllowed() {
115  2 try {
116  2 expect(mock.threeArgumentMethod(1, "2", "3")).andStubAnswer(null);
117  0 fail();
118    } catch (final NullPointerException expected) {
119  2 assertEquals("answer object must not be null", expected.getMessage());
120    }
121    }
122   
 
123    public static class A {
124    }
125   
 
126    public static class B extends A {
127    }
128   
 
129    public static interface C {
130    A foo();
131    }
132   
 
133  2 toggle @Test
134    public void testGenericityFlexibility() {
135   
136  2 final C c = createMock(C.class);
137  2 final B b = new B();
138   
139  2 final IAnswer<B> answer = new IAnswer<B>() {
140   
 
141  4 toggle public B answer() throws Throwable {
142  4 return b;
143    }
144   
145    };
146   
147  2 expect(c.foo()).andAnswer(answer);
148  2 expect(c.foo()).andStubAnswer(answer);
149   
150  2 replay(c);
151  2 assertSame(b, c.foo());
152  2 assertSame(b, c.foo());
153  2 verify(c);
154    }
155   
 
156  2 toggle @Test
157    public void answerOnVoidMethod() {
158  2 final String[] array = new String[] { "a" };
159  2 mock.arrayMethod(array);
160  2 expectLastCall().andAnswer(new IAnswer<Object>() {
 
161  2 toggle public Object answer() throws Throwable {
162  2 final String[] s = (String[]) getCurrentArguments()[0];
163  2 s[0] = "b";
164  2 return null;
165    }
166    });
167  2 replay(mock);
168  2 mock.arrayMethod(array);
169  2 verify(mock);
170   
171  2 assertEquals("b", array[0]);
172    }
173    }