Skip to content
Snippets Groups Projects
Commit 2488dbee authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Ravenwood: Support mockito

Test: atest RavenwoodMockitoTest
Bug: 310268946
Bug: 292141694

Change-Id: I6021e67e12c60b00c54638f43c75350b0d11f0a7
parent 8af29b27
No related branches found
No related tags found
No related merge requests found
package {
// See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import
// all of the 'license_kinds' from "frameworks_base_license"
// to get the below license kinds:
// SPDX-license-identifier-Apache-2.0
default_applicable_licenses: ["frameworks_base_license"],
}
// Ravenwood tests run on the hostside, so we need mockit of the host variant.
// But we need to use it in modules of the android variant, so we "wash" the variant with it.
java_host_for_device {
name: "mockito_ravenwood",
libs: [
"mockito",
"objenesis",
],
}
android_ravenwood_test {
name: "RavenwoodMockitoTest",
srcs: [
"test/**/*.java",
],
static_libs: [
"junit",
"truth",
"mockito_ravenwood",
],
libs: [
"android.test.mock",
"android.test.base",
"android.test.runner",
],
auto_gen_config: true,
}
/*
* Copyright (C) 2023 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import org.junit.Test;
public class MockitoTest {
@Test
public void testMockJdkClass() {
Process object = mock(Process.class);
when(object.exitValue()).thenReturn(42);
assertThat(object.exitValue()).isEqualTo(42);
}
/* It still doesn't work...
STACKTRACE:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class android.content.Intent.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
... But Intent public, non-final.
*/
// @Test
private void testMockAndroidClass1() {
Intent object = mock(Intent.class);
when(object.getAction()).thenReturn("ACTION_RAVENWOOD");
assertThat(object.getAction()).isEqualTo("ACTION_RAVENWOOD");
}
@Test
public void testMockAndroidClass2() {
Context object = mock(Context.class);
when(object.getPackageName()).thenReturn("android");
assertThat(object.getPackageName()).isEqualTo("android");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment