diff options
author | 2024-02-09 11:26:14 -0800 | |
---|---|---|
committer | 2024-02-09 16:14:44 -0800 | |
commit | eeaf655eb422b16f476cd07ee25eedac16a853d9 (patch) | |
tree | 4bc11c2e93aef70908d279cae79a18069b0483c0 | |
parent | d2d7a20f3f18270f9cc35618704173474d556101 (diff) |
Add tests for static mocking
Test: atest RavenwoodMockitoTest && atest RavenwoodMockitoTest_device
Bug: 310268946
Change-Id: I969847f757cbc21e553d7cb1157d5127301f9d10
4 files changed, 90 insertions, 22 deletions
diff --git a/ravenwood/mockito/Android.bp b/ravenwood/mockito/Android.bp index a74bca47f692..95c7394b19f3 100644 --- a/ravenwood/mockito/Android.bp +++ b/ravenwood/mockito/Android.bp @@ -13,6 +13,9 @@ android_ravenwood_test { srcs: [ "test/**/*.java", ], + exclude_srcs: [ + "test/**/*DeviceOnly*.java", + ], static_libs: [ "junit", "truth", @@ -31,6 +34,9 @@ android_test { srcs: [ "test/**/*.java", ], + exclude_srcs: [ + "test/**/*RavenwoodOnly*.java", + ], static_libs: [ "junit", "truth", diff --git a/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoDeviceOnlyTest.java b/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoDeviceOnlyTest.java new file mode 100644 index 000000000000..d02fe69d3168 --- /dev/null +++ b/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoDeviceOnlyTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2024 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.ravenwood.mockito; + +import static com.google.common.truth.Truth.assertThat; + +import android.app.ActivityManager; +import android.platform.test.ravenwood.RavenwoodRule; + +import com.android.dx.mockito.inline.extended.ExtendedMockito; + +import org.junit.Rule; +import org.junit.Test; +import org.mockito.quality.Strictness; + +public class RavenwoodMockitoDeviceOnlyTest { + @Rule public final RavenwoodRule mRavenwood = new RavenwoodRule(); + + @Test + public void testStaticMockOnDevice() { + var mockingSession = ExtendedMockito.mockitoSession() + .strictness(Strictness.LENIENT) + .mockStatic(ActivityManager.class) + .startMocking(); + try { + ExtendedMockito.doReturn(true).when(ActivityManager::isUserAMonkey); + + assertThat(ActivityManager.isUserAMonkey()).isEqualTo(true); + } finally { + mockingSession.finishMocking(); + } + } +} diff --git a/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoRavenwoodOnlyTest.java b/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoRavenwoodOnlyTest.java new file mode 100644 index 000000000000..0c137d5eaacc --- /dev/null +++ b/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoRavenwoodOnlyTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 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.ravenwood.mockito; + +import static com.google.common.truth.Truth.assertThat; + +import android.app.ActivityManager; +import android.platform.test.ravenwood.RavenwoodRule; + +import org.junit.Rule; +import org.junit.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +public class RavenwoodMockitoRavenwoodOnlyTest { + @Rule public final RavenwoodRule mRavenwood = new RavenwoodRule(); + + @Test + public void testStaticMockOnRavenwood() { + try (MockedStatic<ActivityManager> am = Mockito.mockStatic(ActivityManager.class)) { + am.when(ActivityManager::isUserAMonkey).thenReturn(true); + assertThat(ActivityManager.isUserAMonkey()).isEqualTo(true); + } + } +} diff --git a/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoTest.java b/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoTest.java index 1284d64b9a90..95667103bd21 100644 --- a/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoTest.java +++ b/ravenwood/mockito/test/com/android/ravenwood/mockito/RavenwoodMockitoTest.java @@ -31,28 +31,6 @@ import org.junit.Test; public class RavenwoodMockitoTest { @Rule public final RavenwoodRule mRavenwood = new RavenwoodRule(); - -// Use this to mock static methods, which isn't supported by mockito 2. -// Mockito supports static mocking since 3.4.0: -// See: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#48 - -// private MockitoSession mMockingSession; -// -// @Before -// public void setUp() { -// mMockingSession = mockitoSession() -// .strictness(Strictness.LENIENT) -// .mockStatic(RavenwoodMockitoTest.class) -// .startMocking(); -// } -// -// @After -// public void tearDown() { -// if (mMockingSession != null) { -// mMockingSession.finishMocking(); -// } -// } - @Test public void testMockJdkClass() { Process object = mock(Process.class); |