diff options
Diffstat (limited to 'dexmaker-mockito-tests')
-rw-r--r-- | dexmaker-mockito-tests/AndroidManifest.xml | 3 | ||||
-rw-r--r-- | dexmaker-mockito-tests/build.gradle | 40 | ||||
-rw-r--r-- | dexmaker-mockito-tests/src/main/AndroidManifest.xml | 7 | ||||
-rw-r--r-- | dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/BlacklistedApis.java (renamed from dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/BlacklistedApis.java) | 18 | ||||
-rw-r--r-- | dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/CleanStackTrace.java (renamed from dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/CleanStackTrace.java) | 0 | ||||
-rw-r--r-- | dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/GeneralMocking.java (renamed from dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/GeneralMocking.java) | 0 | ||||
-rw-r--r-- | dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/PartialClasses.java | 65 | ||||
-rw-r--r-- | dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/Stress.java | 55 |
8 files changed, 169 insertions, 19 deletions
diff --git a/dexmaker-mockito-tests/AndroidManifest.xml b/dexmaker-mockito-tests/AndroidManifest.xml deleted file mode 100644 index 45201f9..0000000 --- a/dexmaker-mockito-tests/AndroidManifest.xml +++ /dev/null @@ -1,3 +0,0 @@ -<manifest package="com.android.dexmaker.mockito.tests"> - <application /> -</manifest> diff --git a/dexmaker-mockito-tests/build.gradle b/dexmaker-mockito-tests/build.gradle index a08e254..70f3dfa 100644 --- a/dexmaker-mockito-tests/build.gradle +++ b/dexmaker-mockito-tests/build.gradle @@ -1,20 +1,33 @@ -apply plugin: 'com.android.application' +buildscript { + repositories { + maven { + url "https://plugins.gradle.org/m2/" + } + } + dependencies { + classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.13" + } +} + +apply plugin: "net.ltgt.errorprone" +apply plugin: 'com.android.library' android { - compileSdkVersion 25 - buildToolsVersion "25.0.0" + compileSdkVersion 28 - lintOptions { - abortOnError false + android { + lintOptions { + disable 'InvalidPackage' + disable 'NewApi' + } } defaultConfig { - applicationId "com.android.dexmaker.mockito.tests" - minSdkVersion 25 - targetSdkVersion 25 + minSdkVersion 8 + targetSdkVersion 28 versionName VERSION_NAME - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' } } @@ -24,9 +37,10 @@ repositories { } dependencies { - compile project(':dexmaker') - compile project(':dexmaker-mockito') + compileOnly project(':dexmaker-mockito') + androidTestImplementation project(':dexmaker-mockito') - androidTestCompile 'com.android.support.test:runner:0.5' - androidTestCompile 'junit:junit:4.12' + implementation 'com.android.support.test:runner:0.5' + implementation 'junit:junit:4.12' + api 'org.mockito:mockito-core:2.19.0', { exclude group: 'net.bytebuddy' } } diff --git a/dexmaker-mockito-tests/src/main/AndroidManifest.xml b/dexmaker-mockito-tests/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2f3d475 --- /dev/null +++ b/dexmaker-mockito-tests/src/main/AndroidManifest.xml @@ -0,0 +1,7 @@ +<manifest xmlns:tools="http://schemas.android.com/tools" + package="com.android.dexmaker.mockito.tests" + xmlns:android="http://schemas.android.com/apk/res/android"> + <application android:debuggable="true" + tools:ignore="HardcodedDebugMode"/> +</manifest> + diff --git a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/BlacklistedApis.java b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/BlacklistedApis.java index ffe55fb..2144c50 100644 --- a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/BlacklistedApis.java +++ b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/BlacklistedApis.java @@ -23,16 +23,19 @@ import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import android.annotation.SuppressLint; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; +import android.os.Build; import android.provider.Settings; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.view.View; import android.widget.FrameLayout; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,6 +44,11 @@ import org.junit.runner.RunWith; */ @RunWith(AndroidJUnit4.class) public class BlacklistedApis { + @Before + public void onlyRunOnPlatformsThatSupportBlacklisting() { + assumeTrue(Build.VERSION.SDK_INT >= 28); + } + /** * Check if the application is marked as {@code android:debuggable} in the manifest * @@ -84,6 +92,7 @@ public class BlacklistedApis { @Test public void copyBlacklistedFields() throws Exception { + // Can only copy blacklisted fields when debuggable assumeTrue(isDebuggable()); Context targetContext = InstrumentationRegistry.getTargetContext(); @@ -105,6 +114,7 @@ public class BlacklistedApis { parent.measure(100, 100); } + @SuppressLint({"PrivateApi", "CheckReturnValue"}) @Test public void cannotCallBlackListedAfterSpying() { // Spying and mocking might change the View class's byte code @@ -120,8 +130,9 @@ public class BlacklistedApis { } } - public class CallBlackListedMethod { - public boolean callingBlacklistedMethodCausesException() { + public static class CallBlackListedMethod { + @SuppressLint("PrivateApi") + boolean callingBlacklistedMethodCausesException() { // Settings.Global#isValidZenMode is a blacklisted method. Resolving it should fail try { Settings.Global.class.getDeclaredMethod("isValidZenMode", Integer.TYPE); @@ -138,7 +149,8 @@ public class BlacklistedApis { assertTrue(t.callingBlacklistedMethodCausesException()); } - public abstract class CallBlacklistedMethodAbstract { + public static abstract class CallBlacklistedMethodAbstract { + @SuppressLint("PrivateApi") public boolean callingBlacklistedMethodCausesException() { // Settings.Global#isValidZenMode is a blacklisted method. Resolving it should fail try { diff --git a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/CleanStackTrace.java b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/CleanStackTrace.java index 81a3b95..81a3b95 100644 --- a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/CleanStackTrace.java +++ b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/CleanStackTrace.java diff --git a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/GeneralMocking.java b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/GeneralMocking.java index 36d5612..36d5612 100644 --- a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/GeneralMocking.java +++ b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/GeneralMocking.java diff --git a/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/PartialClasses.java b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/PartialClasses.java new file mode 100644 index 0000000..be9d790 --- /dev/null +++ b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/PartialClasses.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dx.mockito.tests; + +import org.junit.Test; +import org.mockito.exceptions.base.MockitoException; + +import java.util.AbstractList; + +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests what happens if code tries to call real methods of abstract classed and in interfaces + */ +public class PartialClasses { + @Test + public void callRealMethodOnInterface() { + Runnable r = mock(Runnable.class); + + try { + doCallRealMethod().when(r).run(); + fail(); + } catch (MockitoException e) { + // expected + } + } + + @Test + public void callAbstractRealMethodOnAbstractClass() { + AbstractList l = mock(AbstractList.class); + + try { + when(l.size()).thenCallRealMethod(); + fail(); + } catch (MockitoException e) { + // expected + } + } + + @Test + public void callRealMethodOnAbstractClass() { + AbstractList l = mock(AbstractList.class); + + doCallRealMethod().when(l).clear(); + + l.clear(); + } +} diff --git a/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/Stress.java b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/Stress.java new file mode 100644 index 0000000..ec4b8e8 --- /dev/null +++ b/dexmaker-mockito-tests/src/main/java/com/android/dx/mockito/tests/Stress.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dx.mockito.tests; + +import android.support.test.runner.AndroidJUnit4; +import android.util.Log; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(AndroidJUnit4.class) +public class Stress { + private static final String LOG_TAG = Stress.class.getSimpleName(); + private static final int NUM_TESTS = 80000; + + public static class TestClass { + public String echo(String in) { + return in; + } + } + + @Test + public void mockALot() { + for (int i = 0; i < NUM_TESTS; i++) { + if (i % 1024 == 0) { + Log.i(LOG_TAG, "Ran " + i + "/" + NUM_TESTS + " tests"); + } + + TestClass m = mock(TestClass.class); + when(m.echo(eq("marco!"))).thenReturn("polo"); + assertEquals("polo", m.echo("marco!")); + verify(m).echo("marco!"); + } + } +} |