diff options
author | Maurice Lam <yukl@google.com> | 2016-11-09 18:47:46 -0800 |
---|---|---|
committer | Maurice Lam <yukl@google.com> | 2016-12-02 10:58:25 -0800 |
commit | a7eb9e54ab12077b00d6088d7d27af8e52a07b83 (patch) | |
tree | 03fc41bfca1e7a55a03efefaa59fca41ad1f6756 | |
parent | af083f11baa423b1aa9968790250fc67df924020 (diff) | |
download | setupwizard-a7eb9e54ab12077b00d6088d7d27af8e52a07b83.tar.gz |
[SuwLib] Set up mockito
Test: ./gradlew connectedAndroidTest
Change-Id: I04ba83f823eba6d8e4893d7f9cabfb710ea4b46d
-rw-r--r-- | library/full-support/test/src/com/android/setupwizardlib/test/HeaderRecyclerViewTest.java | 98 | ||||
-rw-r--r-- | library/self.gradle | 3 |
2 files changed, 20 insertions, 81 deletions
diff --git a/library/full-support/test/src/com/android/setupwizardlib/test/HeaderRecyclerViewTest.java b/library/full-support/test/src/com/android/setupwizardlib/test/HeaderRecyclerViewTest.java index 3fab89d..d9f52cd 100644 --- a/library/full-support/test/src/com/android/setupwizardlib/test/HeaderRecyclerViewTest.java +++ b/library/full-support/test/src/com/android/setupwizardlib/test/HeaderRecyclerViewTest.java @@ -16,7 +16,8 @@ package com.android.setupwizardlib.test; -import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.verify; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; @@ -30,6 +31,8 @@ import com.android.setupwizardlib.view.HeaderRecyclerView.HeaderAdapter; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; /** * Test for {@link com.android.setupwizardlib.view.HeaderRecyclerView} @@ -41,12 +44,14 @@ public class HeaderRecyclerViewTest { private TestAdapter mWrappedAdapter; private HeaderAdapter mHeaderAdapter; - private TestDataObserver mObserver; + @Mock + private RecyclerView.AdapterDataObserver mObserver; @Before public void setUp() { + MockitoAnnotations.initMocks(this); + mWrappedAdapter = new TestAdapter(); - mObserver = new TestDataObserver(); mHeaderAdapter = new HeaderAdapter(mWrappedAdapter); mHeaderAdapter.registerAdapterDataObserver(mObserver); @@ -59,7 +64,7 @@ public class HeaderRecyclerViewTest { public void testNotifyChanged() { mWrappedAdapter.notifyDataSetChanged(); - assertEquals("onChanged", mObserver.lastNotification); + verify(mObserver).onChanged(); } /** @@ -69,9 +74,7 @@ public class HeaderRecyclerViewTest { public void testNotifyItemChangedNoHeader() { mWrappedAdapter.notifyItemChanged(12); - assertEquals("onItemRangeChanged", mObserver.lastNotification); - assertEquals(12, mObserver.lastArg1); - assertEquals(1, mObserver.lastArg2); + verify(mObserver).onItemRangeChanged(eq(12), eq(1), eq(null)); } /** @@ -83,9 +86,7 @@ public class HeaderRecyclerViewTest { mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext())); mWrappedAdapter.notifyItemChanged(12); - assertEquals("onItemRangeChanged", mObserver.lastNotification); - assertEquals(13, mObserver.lastArg1); - assertEquals(1, mObserver.lastArg2); + verify(mObserver).onItemRangeChanged(eq(13), eq(1), eq(null)); } /** @@ -95,9 +96,7 @@ public class HeaderRecyclerViewTest { public void testNotifyItemInsertedNoHeader() { mWrappedAdapter.notifyItemInserted(12); - assertEquals("onItemRangeInserted", mObserver.lastNotification); - assertEquals(12, mObserver.lastArg1); - assertEquals(1, mObserver.lastArg2); + verify(mObserver).onItemRangeInserted(eq(12), eq(1)); } /** @@ -109,9 +108,7 @@ public class HeaderRecyclerViewTest { mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext())); mWrappedAdapter.notifyItemInserted(12); - assertEquals("onItemRangeInserted", mObserver.lastNotification); - assertEquals(13, mObserver.lastArg1); - assertEquals(1, mObserver.lastArg2); + verify(mObserver).onItemRangeInserted(eq(13), eq(1)); } /** @@ -121,9 +118,7 @@ public class HeaderRecyclerViewTest { public void testNotifyItemRemovedNoHeader() { mWrappedAdapter.notifyItemRemoved(12); - assertEquals("onItemRangeRemoved", mObserver.lastNotification); - assertEquals(12, mObserver.lastArg1); - assertEquals(1, mObserver.lastArg2); + verify(mObserver).onItemRangeRemoved(eq(12), eq(1)); } /** @@ -135,9 +130,7 @@ public class HeaderRecyclerViewTest { mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext())); mWrappedAdapter.notifyItemRemoved(12); - assertEquals("onItemRangeRemoved", mObserver.lastNotification); - assertEquals(13, mObserver.lastArg1); - assertEquals(1, mObserver.lastArg2); + verify(mObserver).onItemRangeRemoved(eq(13), eq(1)); } /** @@ -147,10 +140,7 @@ public class HeaderRecyclerViewTest { public void testNotifyItemMovedNoHeader() { mWrappedAdapter.notifyItemMoved(12, 18); - assertEquals("onItemRangeMoved", mObserver.lastNotification); - assertEquals(12, mObserver.lastArg1); - assertEquals(18, mObserver.lastArg2); - assertEquals(1, mObserver.lastArg3); + verify(mObserver).onItemRangeMoved(eq(12), eq(18), eq(1)); } /** @@ -162,10 +152,7 @@ public class HeaderRecyclerViewTest { mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext())); mWrappedAdapter.notifyItemMoved(12, 18); - assertEquals("onItemRangeMoved", mObserver.lastNotification); - assertEquals(13, mObserver.lastArg1); - assertEquals(19, mObserver.lastArg2); - assertEquals(1, mObserver.lastArg3); + verify(mObserver).onItemRangeMoved(eq(13), eq(19), eq(1)); } /** @@ -187,55 +174,4 @@ public class HeaderRecyclerViewTest { return 0; } } - - /** - * Test observer which stores the last observed notification. - */ - // TODO: set up mockito for this instead - public static class TestDataObserver extends RecyclerView.AdapterDataObserver { - - public String lastNotification; - public int lastArg1 = -1; - public int lastArg2 = -1; - public int lastArg3 = -1; - - @Override - public void onChanged() { - super.onChanged(); - lastNotification = "onChanged"; - } - - @Override - public void onItemRangeChanged(int positionStart, int itemCount) { - super.onItemRangeChanged(positionStart, itemCount); - lastNotification = "onItemRangeChanged"; - lastArg1 = positionStart; - lastArg2 = itemCount; - } - - @Override - public void onItemRangeInserted(int positionStart, int itemCount) { - super.onItemRangeInserted(positionStart, itemCount); - lastNotification = "onItemRangeInserted"; - lastArg1 = positionStart; - lastArg2 = itemCount; - } - - @Override - public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { - super.onItemRangeMoved(fromPosition, toPosition, itemCount); - lastNotification = "onItemRangeMoved"; - lastArg1 = fromPosition; - lastArg2 = toPosition; - lastArg3 = itemCount; - } - - @Override - public void onItemRangeRemoved(int positionStart, int itemCount) { - super.onItemRangeRemoved(positionStart, itemCount); - lastNotification = "onItemRangeRemoved"; - lastArg1 = positionStart; - lastArg2 = itemCount; - } - } } diff --git a/library/self.gradle b/library/self.gradle index 7ad6359..e155fee 100644 --- a/library/self.gradle +++ b/library/self.gradle @@ -16,7 +16,10 @@ android.sourceSets { dependencies { androidTestCompile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test:runner:0.5' + androidTestCompile 'com.google.dexmaker:dexmaker:1.2' + androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2' androidTestCompile 'junit:junit:4.+' + androidTestCompile 'org.mockito:mockito-core:1.9.5' } } |