diff --git a/runner/android_junit_runner/java/androidx/test/internal/runner/TestExecutor.java b/runner/android_junit_runner/java/androidx/test/internal/runner/TestExecutor.java index 0100a44f5..b3c1016f5 100644 --- a/runner/android_junit_runner/java/androidx/test/internal/runner/TestExecutor.java +++ b/runner/android_junit_runner/java/androidx/test/internal/runner/TestExecutor.java @@ -16,6 +16,7 @@ package androidx.test.internal.runner; import static androidx.test.internal.util.Checks.checkNotNull; +import static java.nio.charset.StandardCharsets.UTF_8; import android.app.Instrumentation; import android.os.Bundle; @@ -53,7 +54,7 @@ private TestExecutor(Builder builder) { *
If an error occurred during the test execution, the exception will be thrown, and it's the * caller's responsibility to handle the exception properly. */ - public Bundle execute(Request request) throws UnsupportedEncodingException { + public Bundle execute(Request request) { Trace.beginSection("execute tests"); try { return execute(new JUnitCore(), request); @@ -62,7 +63,7 @@ public Bundle execute(Request request) throws UnsupportedEncodingException { } } - Bundle execute(JUnitCore junitRunner, Request request) throws UnsupportedEncodingException { + Bundle execute(JUnitCore junitRunner, Request request) { Bundle resultBundle = new Bundle(); setUpListeners(junitRunner); Result junitResults = junitRunner.run(request); @@ -72,10 +73,15 @@ Bundle execute(JUnitCore junitRunner, Request request) throws UnsupportedEncodin try (PrintStream summaryWriter = new PrintStream(summaryStream)) { reportRunEnded(listeners, summaryWriter, resultBundle, junitResults); } - String summaryOutput = StackTrimmer.getTrimmedSummary(summaryStream.toString("UTF_8")); - resultBundle.putString( - Instrumentation.REPORT_KEY_STREAMRESULT, String.format("\n%s", summaryOutput)); - return resultBundle; + try { + // ByteArrayOutputStream.toString(Charset) requires API level 33 + String summaryOutput = StackTrimmer.getTrimmedSummary(summaryStream.toString(UTF_8.name())); + resultBundle.putString( + Instrumentation.REPORT_KEY_STREAMRESULT, String.format("\n%s", summaryOutput)); + return resultBundle; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("UTF-8 encoding not supported?", e); + } } /** Initialize listeners and add them to the JUnitCore runner */ diff --git a/runner/android_junit_runner/javatests/androidx/test/internal/runner/TestExecutorTest.java b/runner/android_junit_runner/javatests/androidx/test/internal/runner/TestExecutorTest.java index 6e37f3c87..ccdc6f390 100644 --- a/runner/android_junit_runner/javatests/androidx/test/internal/runner/TestExecutorTest.java +++ b/runner/android_junit_runner/javatests/androidx/test/internal/runner/TestExecutorTest.java @@ -25,7 +25,6 @@ import androidx.test.filters.SmallTest; import androidx.test.internal.runner.listener.InstrumentationRunListener; import java.io.PrintStream; -import java.io.UnsupportedEncodingException; import org.junit.Before; import org.junit.Test; import org.junit.runner.Description; @@ -67,7 +66,7 @@ public void run(RunNotifier notifier) {} /** Simple normal case execution */ @Test - public void testExecute() throws UnsupportedEncodingException { + public void testExecute() { executor.execute(mockRequest); verify(mockListener) .instrumentationRunFinished(