diff --git a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php
index 6a8715246a225aa38036ffe78e45830f7e789eb8..61f51dc81a6486f1b3f8d0cc131c6ccf813c8831 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php
@@ -896,15 +896,21 @@ public function testTransactionManagerFailureOnPendingStackItems(): void {
     $reflectionProperty = new \ReflectionProperty(TransactionManagerBase::class, 'connectionTransactionState');
     $reflectionProperty->setValue($manager, ClientConnectionTransactionState::Active);
 
-    $this->expectException(\AssertionError::class);
-    $this->expectExceptionMessageMatches("/^Transaction .stack was not empty\\. Active stack: bar\\\\qux/");
     // Ensure that __destruct() results in an assertion error. Note that this
     // will normally be called by PHP during the object's destruction but Drupal
     // will commit all transactions when a database is closed thereby making
-    // this impossible to test with calling it directly.
-    $manager->__destruct();
+    // this impossible to test unless it is called directly.
+    try {
+      $manager->__destruct();
+      $this->fail("Expected AssertionError error not thrown");
+    }
+    catch (\AssertionError $e) {
+      $this->assertStringStartsWith('Transaction $stack was not empty. Active stack: bar\\qux', $e->getMessage());
+    }
 
     // Clean up.
+    $reflectionProperty = new \ReflectionProperty(TransactionManagerBase::class, 'stack');
+    $reflectionProperty->setValue($manager, []);
     unset($testConnection);
     Database::closeConnection('test_fail');
   }