diff --git a/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php b/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php
index e7fd9cd..19746bb 100644
--- a/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php
+++ b/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php
@@ -159,12 +159,16 @@ protected function assertCacheContexts(array $expected_contexts, $message = NULL
     $actual_contexts = $this->getCacheHeaderValues('X-Drupal-Cache-Contexts');
     sort($expected_contexts);
     sort($actual_contexts);
-    $return = $this->assertIdentical($actual_contexts, $expected_contexts, $message);
-    if (!$return) {
+    $match = $actual_contexts === $expected_contexts;
+    if (!$match) {
       debug('Unwanted cache contexts in response: ' . implode(',', array_diff($actual_contexts, $expected_contexts)));
       debug('Missing cache contexts in response: ' . implode(',', array_diff($expected_contexts, $actual_contexts)));
     }
-    return $return;
+
+    $return = $this->assertIdentical($actual_contexts, $expected_contexts, $message);
+
+    // For compatibility with both BrowserTestBase and WebTestBase always return a boolean.
+    return ($return === NULL) ? TRUE : $return;
   }
 
   /**
