Skip to content

Commit d29b629

Browse files
agrievecopybara-androidxtest
authored andcommitted
Fix exceptions during @afterclass not being reported via InstrumentationResultPrinter
PiperOrigin-RevId: 640969268
1 parent 9375894 commit d29b629

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

runner/android_junit_runner/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
**Bug Fixes**
88

9+
* Exceptions during `@AfterClass` were not being reported via `InstrumentationResultPrinter`.
10+
911
**New Features**
1012

1113
**Breaking Changes**

runner/android_junit_runner/java/androidx/test/internal/runner/listener/InstrumentationResultPrinter.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*/
4545
public class InstrumentationResultPrinter extends InstrumentationRunListener {
4646

47-
private static final String TAG = "InstrumentationResultPrinter";
47+
private static final String TAG = "InstrResultPrinter";
4848

4949
/**
5050
* This value, if stored with key {@link android.app.Instrumentation#REPORT_KEY_IDENTIFIER},
@@ -152,14 +152,12 @@ public void testFinished(Description description) throws Exception {
152152

153153
@Override
154154
public void testFailure(Failure failure) throws Exception {
155-
boolean shouldCallFinish = false;
156-
if (!isAnyTestStarted()) {
157-
// Junit failed during initialization and testStarted was never called. For example, an
158-
// exception was thrown in @BeforeClass method. We must artificially call testStarted and
159-
// testFinished in order to print a descriptive result that external tools (like Studio) can
160-
// understand.
155+
// getMethodName() == null when an exception is thrown during @BeforeClass or @AfterClass.
156+
// No matching testStart() / testFinish() is emitted, so simulate them here for the sake of
157+
// instrumentation consumers.
158+
boolean shouldCallFinish = failure.getDescription().getMethodName() == null;
159+
if (shouldCallFinish) {
161160
testStarted(failure.getDescription());
162-
shouldCallFinish = true;
163161
}
164162
testResultCode = REPORT_VALUE_RESULT_FAILURE;
165163
reportFailure(failure);

runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/BUILD

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,8 @@ axt_android_library_test(
4444
"//ext/junit",
4545
"//runner/android_junit_runner",
4646
"//runner/android_junit_runner/javatests/androidx/test/testing/fixtures",
47-
"//runner/rules",
48-
"//services/events/java/androidx/test/services/events",
49-
"//services/storage",
5047
"@androidsdk//:legacy_test-34",
51-
"@maven//:com_google_guava_guava",
52-
"@maven//:com_google_truth_truth",
5348
"@maven//:junit_junit",
54-
"@maven//:org_hamcrest_hamcrest_core",
55-
"@maven//:org_mockito_mockito_core",
5649
],
5750
)
5851

runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/InstrumentationResultPrinterTest.java

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@
2323
import android.os.Bundle;
2424
import androidx.test.ext.junit.runners.AndroidJUnit4;
2525
import androidx.test.filters.SmallTest;
26+
import java.util.ArrayList;
27+
import java.util.List;
2628
import junit.framework.Assert;
29+
import org.junit.AfterClass;
30+
import org.junit.BeforeClass;
2731
import org.junit.Test;
32+
import org.junit.runner.Computer;
2833
import org.junit.runner.Description;
34+
import org.junit.runner.JUnitCore;
35+
import org.junit.runner.Request;
2936
import org.junit.runner.RunWith;
30-
import org.junit.runner.notification.Failure;
3137

3238
@RunWith(AndroidJUnit4.class)
3339
@SmallTest
@@ -62,27 +68,50 @@ public void sendStatus(int code, Bundle bundle) {
6268
assertTrue(resultBundle[0].containsKey(REPORT_KEY_STACK));
6369
}
6470

65-
@Test
66-
public void verifyFailureDescriptionPropagatedToStartAndFinishMethods() throws Exception {
67-
Description[] descriptions = new Description[2];
68-
InstrumentationResultPrinter intrResultPrinter =
69-
new InstrumentationResultPrinter() {
70-
@Override
71-
public void testStarted(Description description) throws Exception {
72-
descriptions[0] = description;
73-
}
71+
private static class TestInstrumentationResultPrinter extends InstrumentationResultPrinter {
72+
final List<String> resultsLog = new ArrayList<>();
7473

75-
@Override
76-
public void testFinished(Description description) throws Exception {
77-
descriptions[1] = description;
78-
}
79-
};
74+
@Override
75+
public void sendStatus(int code, Bundle bundle) {
76+
resultsLog.add(
77+
String.format(
78+
"code=%s, name=%s#%s",
79+
code,
80+
bundle.getString(REPORT_KEY_NAME_CLASS),
81+
bundle.getString(REPORT_KEY_NAME_TEST)));
82+
}
83+
}
84+
85+
public static class ThrowingTest {
86+
@BeforeClass
87+
public static void setUpClass() {
88+
throw new RuntimeException();
89+
}
90+
91+
@AfterClass
92+
public static void tearDownClass() {
93+
throw new RuntimeException();
94+
}
8095

81-
Description d = Description.createTestDescription(this.getClass(), "Failure Description");
82-
Failure testFailure = new Failure(d, new Exception());
83-
intrResultPrinter.testFailure(testFailure);
96+
@Test
97+
public void emptyTest() {}
98+
}
99+
100+
@Test
101+
public void verifyBeforeClassExceptionsReported() throws Exception {
102+
JUnitCore core = new JUnitCore();
103+
var intrResultPrinter = new TestInstrumentationResultPrinter();
104+
core.addListener(intrResultPrinter);
105+
Request testRequest = Request.classes(new Computer(), new Class<?>[] {ThrowingTest.class});
106+
core.run(testRequest);
84107

85-
assertEquals(d, descriptions[0]);
86-
assertEquals(d, descriptions[1]);
108+
String className = ThrowingTest.class.getName();
109+
assertEquals(
110+
List.of(
111+
"code=1, name=" + className + "#null",
112+
"code=-2, name=" + className + "#null",
113+
"code=1, name=" + className + "#null",
114+
"code=-2, name=" + className + "#null"),
115+
intrResultPrinter.resultsLog);
87116
}
88117
}

0 commit comments

Comments
 (0)