Skip to content

Instantly share code, notes, and snippets.

@akeemphilbert
Last active August 29, 2015 14:08
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 akeemphilbert/824b6cb72510de5ecd10 to your computer and use it in GitHub Desktop.
Save akeemphilbert/824b6cb72510de5ecd10 to your computer and use it in GitHub Desktop.
Android integration between robolectric + roboguice 3 + mockito. Mocks can be created in tests using the @mock annotation and injected via roboguice. Note This setup relies on having a custom Application class set and having a corresponding TestApplication for Robolectric to load. You should also have roboguice 3 and mockito installed as well
/**
Copyright (c) 2014, Akeem Philbert
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
package com.example.app.util;
import com.google.inject.AbstractModule;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MockitoGuiceModule extends AbstractModule {
private HashMap<Class<?>, Object> bindings;
public MockitoGuiceModule() {
super();
bindings = new HashMap<Class<?>, Object>();
}
public void addBinding(Class<?> type, Object object) {
bindings.put(type, object);
}
@Override
protected void configure() {
Set<Map.Entry<Class<?>, Object>> entries = bindings.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
binder.bind((Class<Object>) entry.getKey()).toInstance(entry.getValue());
}
}
}
/**
Copyright (c) 2014, Akeem Philbert
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
package com.example.app;
import com.example.app.util.MockitoGuiceModule;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.TestLifecycleApplication;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import roboguice.RoboGuice;
/**
* Test application
*/
public class TestApplication extends Application implements TestLifecycleApplication {
@Override
public void beforeTest(Method method) {
}
@Override
public void prepareTest(Object test) {
TestApplication application = (TestApplication) Robolectric.application;
MockitoAnnotations.initMocks(test);//initialize all the mocks that were specified using annotations
MockitoGuiceModule mockitoGuiceModule = new MockitoGuiceModule();
try {
//get the list of mock objects
List<Object> mocks = getListOfMocks(test);
//create bindings for all the classes of the mock objects to use the mock object
for (final Object mock : mocks) {
mockitoGuiceModule.addBinding(mock.getClass().getSuperclass(), mock);
}
} catch (IllegalAccessException e) {
}
//override the default roboguice module that does injection with our own module that will inject mock objects
RoboGuice.overrideApplicationInjector(application,mockitoGuiceModule);
}
@Override
public void afterTest(Method method) {
RoboGuice.Util.reset();
}
/**
* Get a list of all the mock objects in the test case being run
*
* @param test
* @return
* @throws IllegalAccessException
*/
private List<Object> getListOfMocks(Object test) throws IllegalAccessException {
final Field[] declaredFields = test.getClass().getDeclaredFields();
List<Object> objects = new ArrayList<Object>();
for (Field field : declaredFields) {
if (field.getAnnotation(Mock.class) != null) {
field.setAccessible(true);
objects.add(field.get(test));
}
}
return objects;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment