Permalink
Browse files

Testing adding Mockito mock with InjectMock annotation

  • Loading branch information...
1 parent 0342d96 commit d9e35013ec71b98a812f05fc29a00c83fadc50e0 Glenn Jahnke and Ryan Spore committed Feb 12, 2013
@@ -4,7 +4,9 @@
<root url="file://$PROJECT_DIR$/libs/test" />
</CLASSES>
<JAVADOC />
- <SOURCES />
+ <SOURCES>
+ <root url="jar://$PROJECT_DIR$/libs/test/mockito-core-1.9.5.jar!/" />
+ </SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/libs/test" recursive="true" />
</library>
</component>
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -24,4 +24,8 @@ public void onCreate(Bundle savedInstanceState) {
title.setText("Hello World");
currentTimeText.setText(String.valueOf(currentTime.currentTimeMillis()));
}
+
+ public CurrentTime getCurrentTime() {
+ return currentTime;
+ }
}
@@ -12,8 +12,7 @@
@RunWith(RobolectricTestRunnerWithInjection.class)
public class StarterActivityWithRoboguiceTest {
- @Inject
- CurrentTime currentTime;
+ @Inject CurrentTime currentTime;
@Test
public void testCurrentTimeIsInjected() throws Exception {
@@ -0,0 +1,12 @@
+package com.example.android.sampleapp.test.support;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target(FIELD)
+@Retention(RUNTIME)
+public @interface InjectMock {
+}
@@ -0,0 +1,34 @@
+package com.example.android.sampleapp.test.support;
+
+
+import org.mockito.Mockito;
+import org.mockito.exceptions.base.MockitoException;
+import org.mockito.internal.util.reflection.FieldSetter;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+
+public class InjectMockAnnotations {
+ public static void initInjectMocks(Class testClass, InjectMockModule injectMockModule, Object testInstance) {
+ Field[] fields = testClass.getDeclaredFields();
+ for (Field field : fields) {
+ for (Annotation annotation : field.getAnnotations()) {
+ if (annotation.annotationType() == InjectMock.class) {
+ addMockToModule(field, injectMockModule, testInstance);
+ }
+ }
+ }
+ }
+
+ private static <T extends Object> void addMockToModule(Field field, InjectMockModule injectMockModule, Object testInstance) {
+ Class<T> type = (Class<T>) field.getType();
+ T mock = Mockito.mock(type);
+ injectMockModule.addBindingForMock(type, mock);
+ try {
+ new FieldSetter(testInstance, field).set(mock);
+ } catch (Exception e) {
+ throw new MockitoException("Problems setting field " + field.getName() + " annotated with "
+ + InjectMock.class, e);
+ }
+ }
+}
@@ -0,0 +1,42 @@
+package com.example.android.sampleapp.test.support;
+
+import com.example.android.sampleapp.MySampleApplication;
+import com.example.android.sampleapp.StarterActivity;
+import com.example.android.sampleapp.util.CurrentTime;
+import com.xtremelabs.robolectric.Robolectric;
+import com.xtremelabs.robolectric.RobolectricTestRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import roboguice.RoboGuice;
+
+import static com.pivotallabs.robolectricgem.expect.Expect.expect;
+import static org.mockito.Mockito.verify;
+
+@RunWith(RobolectricTestRunner.class)
+public class InjectMockAnnotationsTest {
+ @InjectMock CurrentTime mockCurrentTime;
+
+ private void injectMocks() {
+ MySampleApplication application = (MySampleApplication) Robolectric.application;
+ InjectMockModule module = new InjectMockModule();
+ InjectMockAnnotations.initInjectMocks(this.getClass(), module, this);
+
+ RoboGuice.setBaseApplicationInjector(application, RoboGuice.DEFAULT_STAGE,
+ RoboGuice.newDefaultRoboModule(application),
+ module);
+
+ RoboGuice.getInjector(application).injectMembers(this);
+ }
+
+ @Test
+ public void fieldAnnotatedWith_InjectMock_shouldBeSetToMocksOfTheCorrectType() throws Exception {
+ injectMocks();
+ StarterActivity myActivity = new StarterActivity();
+
+ myActivity.onCreate(null);
+
+ verify(mockCurrentTime).currentTimeMillis();
+
+ expect(myActivity.getCurrentTime()).toEqual(mockCurrentTime);
+ }
+}
@@ -0,0 +1,20 @@
+package com.example.android.sampleapp.test.support;
+
+import com.google.inject.AbstractModule;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class InjectMockModule extends AbstractModule {
+ Map<Class, Object> bindingMap = new HashMap<Class, Object>();
+ @Override
+ protected void configure() {
+ for (Class mockClass : bindingMap.keySet()) {
+ bind(mockClass).toInstance(bindingMap.get(mockClass));
+ }
+ }
+
+ public <T> void addBindingForMock(Class<T> mockClass, T mock) {
+ bindingMap.put(mockClass, mock);
+ }
+}
@@ -0,0 +1,20 @@
+package com.example.android.sampleapp.test.support;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+
+import java.util.ArrayList;
+
+import static org.mockito.Mockito.verify;
+
+@RunWith(RobolectricTestRunnerWithInjection.class)
+public class MockitoTest {
+ @Mock ArrayList mockList;
+
+ @Test
+ public void testMockitoWorks() throws Exception {
+ mockList.add("foo");
+ verify(mockList).add("foo");
+ }
+}
@@ -6,6 +6,7 @@
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;
import org.junit.runners.model.InitializationError;
+import org.mockito.MockitoAnnotations;
import roboguice.RoboGuice;
public class RobolectricTestRunnerWithInjection extends RobolectricTestRunner {
@@ -23,6 +24,7 @@ public void prepareTest(Object test) {
RoboGuice.newDefaultRoboModule(application), new TestApplicationModule());
RoboGuice.getInjector(application).injectMembers(test);
+ MockitoAnnotations.initMocks(test);
}
public static class TestApplicationModule extends ApplicationModule {

0 comments on commit d9e3501

Please sign in to comment.