1 /** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 package cpptools; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 import junit.framework.TestCase; 34 import org.easymock.classextension.EasyMock; 35 36 /** 37 * Extends a JUnit TestCase with several EasyMock related features. 38 * <p> 39 * This extension provides the following benefits : 40 * <ul> 41 * <li>supports interface and concrete classes through the same creation method 42 * <li>resets all expectations before each test 43 * <li>verifies all expectations after each test 44 * <li>provides helper methods to reset, replay or verify all control objects at once 45 * </ul> 46 * <p> 47 * As a consequence the tests are easier to write and end up being simpler. 48 * <p> 49 * TODO add nice and strict mock support 50 * 51 * @see <a href="http://junit.org">JUnit</a> 52 * @see <a href="http://easymock.org">EasyMock</a> 53 * @author Mathieu Champlon 54 */ 55 public class EasyMockTestCase extends TestCase 56 { 57 /** 58 * The mock objects. 59 */ 60 private final List<Object> mocks = new ArrayList<Object>(); 61 /** 62 * Whether a forced replay is needed before verifying all controls. 63 */ 64 private boolean mustForceReplay = true; 65 66 /** 67 * {@inheritDoc} 68 */ 69 public final void runBare() throws Throwable 70 { 71 try 72 { 73 setUp(); 74 reset(); 75 try 76 { 77 runTest(); 78 } 79 finally 80 { 81 tearDown(); 82 } 83 if( mustForceReplay ) 84 replay(); 85 verify(); 86 } 87 finally 88 { 89 mocks.clear(); 90 mustForceReplay = true; 91 } 92 } 93 94 /** 95 * Factory method to create a mock object of a given type. 96 * 97 * @param <T> the type of the created mock object 98 * @param type the type of the mock object to create 99 * @return the created mock object 100 */ 101 protected final <T> T createMock( final Class<T> type ) 102 { 103 final T mock = EasyMock.createMock( type ); 104 mocks.add( mock ); 105 return mock; 106 } 107 108 /** 109 * Reset all mock objects expectations. 110 * <p> 111 * The state of mock objects is then the same as at the beginning of the test. 112 */ 113 protected final void reset() 114 { 115 mustForceReplay = true; 116 for( Object mock : mocks ) 117 EasyMock.reset( mock ); 118 } 119 120 /** 121 * Set all mock objects to replay mode. 122 */ 123 protected final void replay() 124 { 125 mustForceReplay = false; 126 for( Object mock : mocks ) 127 EasyMock.replay( mock ); 128 } 129 130 /** 131 * Verify all mock objects expectations. 132 * <p> 133 * This method is automatically called at the end of each test. 134 */ 135 protected final void verify() 136 { 137 for( Object mock : mocks ) 138 EasyMock.verify( mock ); 139 } 140 141 /** 142 * This method serves no purpose but to deactivate the JUnit warning "No tests found in tools.EasyMockTestCase". 143 */ 144 public final void testMethodToRemoveJUnitWarningAboutMissingTests() 145 { 146 } 147 }