Skip to content

Instantly share code, notes, and snippets.

@russelldb
Created November 6, 2009 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russelldb/227866 to your computer and use it in GitHub Desktop.
Save russelldb/227866 to your computer and use it in GitHub Desktop.
/**
*
*/
package net.ossme.test.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.easymock.classextension.EasyMock;
import org.apache.tapestry5.ioc.annotations.Inject;
/**
* Checks the fields of passed class for the @Mock annotation and creates a mock
* and sets the field to that value
*
* @author russell
*
*/
public class EasyMockHelper {
private final Object testClass;
private final Object objectUnderTest;
private List<Object> mocks = new ArrayList<Object>();
public EasyMockHelper(final Object testClass) {
this.testClass = testClass;
this.objectUnderTest = null;
init();
}
public EasyMockHelper(final Object testClass, final Object objectUnderTest) {
this.testClass = testClass;
this.objectUnderTest = objectUnderTest;
init();
}
private void init() {
Class clazz = this.testClass.getClass();
init(clazz);
if (objectUnderTest != null) {
inject(this.objectUnderTest.getClass());
}
}
/**
* Sort of inject the mocks in a haphazard way into the object under test
*
* @param clazz
*/
private void inject(Class clazz) {
if (Object.class.equals(clazz)) {
return;
}
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
Autowired annotation = field.getAnnotation(Inject.class);
if (annotation != null) {
// loop over the mocks and chose the first one that matches
for (Object mock : mocks) {
if (field.getType().isAssignableFrom(mock.getClass())) {
// match
field.setAccessible(true);
// set
try {
field.set(objectUnderTest, mock);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Failed to 'inject' mock into field " + field.getName(), e);
}
// break
break;
}
}
}
}
inject(clazz.getSuperclass());
}
private void init(Class clazz) {
if (Object.class.equals(clazz)) {
return;
}
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Mock annotation = field.getAnnotation(Mock.class);
if (annotation != null) {
Object mock = EasyMock.createMock(field.getType());
try {
field.set(testClass, mock);
mocks.add(mock);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Failed to set field " + field.getName(), e);
}
}
}
init(clazz.getSuperclass());
}
/**
* Replay all mocks
*/
public void replayMocks() {
EasyMock.replay(mocks.toArray(new Object[mocks.size()]));
}
/**
* Verify all mocks
*/
public void verifyMocks() {
EasyMock.verify(mocks.toArray(new Object[mocks.size()]));
}
/**
* Reset all mocks
*/
public void resetMocks() {
EasyMock.reset(mocks.toArray(new Object[mocks.size()]));
}
}
/**
*
*/
package net.ossme.test.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* Annotate a field as being of type Mock. Used by EasyMockHelper
* @author russell
*
*/
@Retention( RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Mock {
}
package net.ossme.test.example;
import static org.easymock.EasyMock.expect;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import net.ossme.test.util.EasyMockHelper;
import net.ossme.test.util.Mock;
public class ExampleTest {
private EasyMockHelper mockHelper;
@Mock
private MyT5ServiceThatIwantToMock serviceInstance;
private MyT5Page page;
@SuppressWarnings("unused")
@BeforeClass(alwaysRun = true)
private void setUp() {
page = new MyT4Page();
mockHelper = new EasyMockHelper(this, page);
}
@SuppressWarnings("unused")
@AfterMethod(alwaysRun = true)
private void resetMocks() {
mockHelper.resetMocks();
}
@Test(groups = { "fast" })
public void testSomeThing() throws Exception {
expect(serviceInstance.getThing()).andReturn(thing);
mockHelper.replayMocks();
page.methodCall();
mockHelper.verifyMocks();
//the rest of the stuff you want to assert and all that
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment