001: /* MockUtils.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License version 2.1 as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, visit
017: * http://www.opensource.org/licenses/lgpl-license.php
018: */
019: package org.ddsteps.mock;
020:
021: import org.easymock.MockControl;
022:
023: /**
024: * Utilities for working with EasyMock.
025: *
026: * @author Adam Skogman
027: */
028: public class MockUtils {
029:
030: private static MockUtilsBean bean = new MockUtilsBean();
031:
032: /**
033: * Creates a class mock, mocking all abstract methods and unimplemented inferface methods. Useful when you want to
034: * test an abstract class that calls abstract methods or methods in an interface that are not implemented.
035: *
036: * @param clazz The class
037: * @return MockControl, not null.
038: */
039: public static MockControl createAbstractStrictClassControl(
040: Class clazz) {
041: return bean.createAbstractStrictClassControl(clazz);
042: }
043:
044: /**
045: * Creates a class mock, mocking all methods in super classes and any remaining unimplemented abstract/interface
046: * methods. Please note that (@link Object#equals(java.lang.Object)), (@link Object#toString()) and (@link
047: * Object#hashCode()) will never be mocked, as we are using EasyMock. If you have overriden these, the override will
048: * not be mocked either. If your overriden method calls any other method (which may be mocked) you need to handle
049: * that!
050: *
051: * @param clazz
052: * @return Not null
053: */
054: public static MockControl createFocusedStrictClassControl(
055: Class clazz) {
056: return bean.createFocusedStrictClassControl(clazz);
057: }
058:
059: /**
060: * @param clazz
061: * @param methodName
062: * @return Never null.
063: * @throws NoSuchMethodException
064: */
065: public static MockControl createMethodFocusedStrictClassControl(
066: final Class clazz, String methodName)
067: throws NoSuchMethodException {
068: return bean.createMethodFocusedStrictClassControl(clazz,
069: methodName);
070: }
071:
072: /**
073: * You may use this, if you want to use the implementation rather than stick to these static methods.
074: *
075: * @return Returns the implementation bean.
076: */
077: public static MockUtilsBean getBean() {
078: return bean;
079: }
080:
081: /**
082: * Replays all MockControls in a TestCase. Finds all fields (private even) and calls them.
083: *
084: * @param testCase The object to browse for MockControls.
085: * @return The number of MockControls handled.
086: */
087: public static int replay(Object testCase) {
088: return bean.replay(testCase);
089: }
090:
091: /**
092: * Sets the implementing bean.
093: *
094: * @param bean The bean.
095: */
096: public static void setBean(MockUtilsBean bean) {
097: MockUtils.bean = bean;
098: }
099:
100: /**
101: * Verifies all MockControls in a TestCase. Finds all fields (private even) and calls them.
102: *
103: * @param testCase The object to browse for MockControls.
104: * @return The number of MockControls handled.
105: */
106: public static int verify(Object testCase) {
107: return bean.verify(testCase);
108: }
109:
110: /**
111: * Don't create me.
112: */
113: private MockUtils() {
114: }
115: }
|