diff options
| author | 2024-09-19 20:47:43 +0000 | |
|---|---|---|
| committer | 2024-09-27 18:24:08 +0000 | |
| commit | 2d37d6e39f55471cb78b1baad82f451521eb455c (patch) | |
| tree | d96b111761eb4e08edb4d27d5be54fa4280fe7c2 | |
| parent | 2fe038507810606fbdd586365f81ffca52e6ff0b (diff) | |
Fixed TestWithLooperRule switch for UiThreadStatement and ExpectException
When switching from AndroidTestRunner to AndroidJUnit4, the annotation @Test(expected = <Exception> wrap the test method, which is delegated out to AndroidTestRunner if it's there, but a switch statement in wrapMethodInStatement is called in the AndroidJUnit4 case, which caused an exception for the uhandled case.
Bug: 352170965
Test: manual test with atest
Flag: TEST_ONLY
Change-Id: Ib45be25234519a22031b5774e9c83fcad59c1c47
| -rw-r--r-- | tests/testables/Android.bp | 5 | ||||
| -rw-r--r-- | tests/testables/src/android/testing/TestWithLooperRule.java | 5 | ||||
| -rw-r--r-- | tests/testables/tests/Android.bp | 1 | ||||
| -rw-r--r-- | tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java | 42 |
4 files changed, 51 insertions, 2 deletions
diff --git a/tests/testables/Android.bp b/tests/testables/Android.bp index 7596ee722d01..f2111856c666 100644 --- a/tests/testables/Android.bp +++ b/tests/testables/Android.bp @@ -25,7 +25,10 @@ package { java_library { name: "testables", - srcs: ["src/**/*.java"], + srcs: [ + "src/**/*.java", + "src/**/*.kt", + ], libs: [ "android.test.runner.stubs.system", "android.test.mock.stubs.system", diff --git a/tests/testables/src/android/testing/TestWithLooperRule.java b/tests/testables/src/android/testing/TestWithLooperRule.java index 37b39c314e53..10df17f991d3 100644 --- a/tests/testables/src/android/testing/TestWithLooperRule.java +++ b/tests/testables/src/android/testing/TestWithLooperRule.java @@ -34,13 +34,13 @@ import java.util.List; * Looper for the Statement. */ public class TestWithLooperRule implements MethodRule { - /* * This rule requires to be the inner most Rule, so the next statement is RunAfters * instead of another rule. You can set it by '@Rule(order = Integer.MAX_VALUE)' */ @Override public Statement apply(Statement base, FrameworkMethod method, Object target) { + // getting testRunner check, if AndroidTestingRunning then we skip this rule RunWith runWithAnnotation = target.getClass().getAnnotation(RunWith.class); if (runWithAnnotation != null) { @@ -97,6 +97,9 @@ public class TestWithLooperRule implements MethodRule { case "InvokeParameterizedMethod": this.wrapFieldMethodFor(next, "frameworkMethod", method, target); return; + case "ExpectException": + next = this.getNextStatement(next, "next"); + break; default: throw new Exception( String.format("Unexpected Statement received: [%s]", diff --git a/tests/testables/tests/Android.bp b/tests/testables/tests/Android.bp index 1eb36fa5f908..c23f41a6c3d4 100644 --- a/tests/testables/tests/Android.bp +++ b/tests/testables/tests/Android.bp @@ -34,6 +34,7 @@ android_test { "androidx.core_core-animation", "androidx.core_core-ktx", "androidx.test.rules", + "androidx.test.ext.junit", "hamcrest-library", "mockito-target-inline-minus-junit4", "testables", diff --git a/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java b/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java new file mode 100644 index 000000000000..b7d5e0e12942 --- /dev/null +++ b/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java @@ -0,0 +1,42 @@ +/* + * 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 android.testing; + +import android.testing.TestableLooper.RunWithLooper; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test that TestableLooper now handles expected exceptions in tests + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +@RunWithLooper +public class TestableLooperJUnit4Test { + @Rule + public final TestWithLooperRule mTestWithLooperRule = new TestWithLooperRule(); + + @Test(expected = Exception.class) + public void testException() throws Exception { + throw new Exception("this exception is expected"); + } +} + |