diff options
author | Philip P. Moltmann <moltmann@google.com> | 2018-03-13 13:11:07 -0700 |
---|---|---|
committer | Philip P. Moltmann <moltmann@google.com> | 2018-03-15 12:33:59 -0700 |
commit | d4a20568bcbaa34526676e4758e011ea32784825 (patch) | |
tree | 2fefe4d0032fd648c0024fd5258496dfae7ad1bb /dexmaker-mockito-tests | |
parent | a0553730d3fb78f907bdb79a270fdfc43b59ace5 (diff) | |
download | dexmaker-d4a20568bcbaa34526676e4758e011ea32784825.tar.gz |
Update dexmaker to almost 2.16
Test: atest CtsMockingTestCases CtsInlineMockingTestCases
Bug: 74344734
Change-Id: Ib79431a454b3ae9715906da61fc9267d54ae7f07
Diffstat (limited to 'dexmaker-mockito-tests')
-rw-r--r-- | dexmaker-mockito-tests/build.gradle | 1 | ||||
-rw-r--r-- | dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/GeneralMocking.java | 172 |
2 files changed, 173 insertions, 0 deletions
diff --git a/dexmaker-mockito-tests/build.gradle b/dexmaker-mockito-tests/build.gradle index f0befe0..a08e254 100644 --- a/dexmaker-mockito-tests/build.gradle +++ b/dexmaker-mockito-tests/build.gradle @@ -20,6 +20,7 @@ android { repositories { jcenter() + google() } dependencies { diff --git a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/GeneralMocking.java b/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/GeneralMocking.java index 6f824f2..fa01df4 100644 --- a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/GeneralMocking.java +++ b/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/GeneralMocking.java @@ -18,13 +18,24 @@ package com.android.dx.mockito.tests; import android.support.test.runner.AndroidJUnit4; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.exceptions.base.MockitoException; import org.mockito.exceptions.verification.NoInteractionsWanted; +import java.util.ArrayList; +import java.util.Arrays; + +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -36,10 +47,32 @@ public class GeneralMocking { public String returnA() { return "A"; } + + public String throwThrowable() throws Throwable { + throw new Throwable(); + } + + public String throwOutOfMemoryError() throws OutOfMemoryError { + throw new OutOfMemoryError(); + } + + public void throwNullPointerException() { + throw new NullPointerException(); + } + + public String concat(String a, String b) { + return a + b; + } + } + + public static class TestSubClass extends TestClass { + } public interface TestInterface { String returnA(); + + String concat(String a, String b); } @Test @@ -103,7 +136,146 @@ public class GeneralMocking { assertTrue(e.getMessage(), e.getMessage().contains(here.getStackTrace()[0].getMethodName())); } + } + } + + @Test + public void spyThrowingMethod() throws Exception { + TestClass t = spy(TestClass.class); + + try { + t.throwThrowable(); + } catch (Throwable e) { + assertEquals("throwThrowable", e.getStackTrace()[0].getMethodName()); + return; + } + + fail(); + } + @Test() + public void spyErrorMethod() throws Exception { + TestClass t = spy(TestClass.class); + + try { + t.throwOutOfMemoryError(); + fail(); + } catch (OutOfMemoryError e) { + assertEquals("throwOutOfMemoryError", e.getStackTrace()[0].getMethodName()); + } + } + + @Test() + public void spyExceptingMethod() throws Exception { + TestClass t = spy(TestClass.class); + + try { + t.throwNullPointerException(); + fail(); + } catch (NullPointerException e) { + assertEquals("throwNullPointerException", e.getStackTrace()[0].getMethodName()); } } + + + @Test + public void callAbstractRealMethod() throws Exception { + TestInterface t = mock(TestInterface.class); + + try { + when(t.returnA()).thenCallRealMethod(); + } catch (MockitoException e) { + assertEquals("callAbstractRealMethod", e.getStackTrace()[0].getMethodName()); + } + } + + @Test + public void callInterfaceWithoutMatcher() throws Exception { + TestInterface t = mock(TestInterface.class); + + when(t.concat("a", "b")).thenReturn("match"); + + assertEquals("match", t.concat("a", "b")); + assertNull(t.concat("b", "a")); + } + + @Test + public void callInterfaceWithMatcher() throws Exception { + TestInterface t = mock(TestInterface.class); + + when(t.concat(eq("a"), anyString())).thenReturn("match"); + + assertEquals("match", t.concat("a", "b")); + assertNull(t.concat("b", "a")); + } + + @Test + public void callInterfaceWithNullMatcher() throws Exception { + TestInterface t = mock(TestInterface.class); + + when(t.concat(eq("a"), (String) isNull())).thenReturn("match"); + + assertEquals("match", t.concat("a", null)); + assertNull(t.concat("a", "b")); + } + + @Test + public void callClassWithoutMatcher() throws Exception { + TestClass t = spy(TestClass.class); + + when(t.concat("a", "b")).thenReturn("match"); + + assertEquals("match", t.concat("a", "b")); + assertEquals("ba", t.concat("b", "a")); + } + + @Test + public void callClassWithMatcher() throws Exception { + TestClass t = spy(TestClass.class); + + when(t.concat(eq("a"), anyString())).thenReturn("match"); + + assertEquals("match", t.concat("a", "b")); + assertEquals("ba", t.concat("b", "a")); + } + + @Test + public void callClassWithNullMatcher() throws Exception { + TestClass t = spy(TestClass.class); + + when(t.concat(eq("a"), (String) isNull())).thenReturn("match"); + + assertEquals("match", t.concat("a", null)); + assertEquals("ab", t.concat("a", "b")); + } + + @Test + public void callSubClassWithoutMatcher() throws Exception { + TestSubClass t = spy(TestSubClass.class); + + when(t.concat("a", "b")).thenReturn("match"); + + assertEquals("match", t.concat("a", "b")); + assertEquals("ba", t.concat("b", "a")); + } + + @Test + public void callSubClassWithMatcher() throws Exception { + TestSubClass t = spy(TestSubClass.class); + + when(t.concat(eq("a"), anyString())).thenReturn("match"); + + assertEquals("match", t.concat("a", "b")); + assertEquals("ba", t.concat("b", "a")); + } + + @Test + public void callSubClassWithNullMatcher() throws Exception { + TestSubClass t = spy(TestSubClass.class); + + when(t.concat(eq("a"), (String) isNull())).thenReturn("match"); + + assertEquals("match", t.concat("a", null)); + assertEquals("ab", t.concat("a", "b")); + } } |