Skip to content

Commit

Permalink
Fix exceptions during @afterclass not being reported via Instrumentat…
Browse files Browse the repository at this point in the history
…ionResultPrinter

PiperOrigin-RevId: 637095159
  • Loading branch information
agrieve authored and copybara-androidxtest committed May 27, 2024
1 parent 4b9c19f commit fa01c1d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions runner/android_junit_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
`androidx.test:runner:{version}` is released.

**Bug Fixes**
* Exceptions during `@AfterClass` were not being reported via `InstrumentationResultPrinter`.

**New Features**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,18 @@ public void testFinished(Description description) throws Exception {

@Override
public void testFailure(Failure failure) throws Exception {
boolean shouldCallFinish = false;
if (!isAnyTestStarted()) {
// Exception thrown during @BeforeClass or @AfterClass.
boolean shouldCallStartAndFinish = failure.getDescription().getMethodName() == null;
if (shouldCallStartAndFinish) {
// Junit failed during initialization and testStarted was never called. For example, an
// exception was thrown in @BeforeClass method. We must artificially call testStarted and
// testFinished in order to print a descriptive result that external tools (like Studio) can
// understand.
testStarted(failure.getDescription());
shouldCallFinish = true;
}
testResultCode = REPORT_VALUE_RESULT_FAILURE;
reportFailure(failure);
if (shouldCallFinish) {
if (shouldCallStartAndFinish) {
testFinished(failure.getDescription());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testFinished(Description description) throws Exception {
}
};

Description d = Description.createTestDescription(this.getClass(), "Failure Description");
Description d = Description.createSuiteDescription(this.getClass());
Failure testFailure = new Failure(d, new Exception());
intrResultPrinter.testFailure(testFailure);

Expand Down

0 comments on commit fa01c1d

Please sign in to comment.