diff --git a/core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php b/core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php
index 48cc862..1005301 100644
--- a/core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php
+++ b/core/modules/comment/tests/Drupal/comment/Tests/Entity/CommentLockTest.php
@@ -74,15 +74,6 @@ public function testLocks() {
     $comment->postSave($storage_controller);
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  protected function tearDown() {
-    parent::tearDown();
-    $container = new ContainerBuilder();
-    \Drupal::setContainer($container);
-  }
-
 }
 }
 namespace {
diff --git a/core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTest.php b/core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTest.php
index aaaa625..d590e06 100644
--- a/core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTest.php
+++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTest.php
@@ -46,12 +46,6 @@ protected function setUp() {
     \Drupal::setContainer($container);
   }
 
-  protected function tearDown() {
-    // Passes in an empty container.
-    $container = new ContainerBuilder();
-    \Drupal::setContainer($container);
-  }
-
   /**
    * Sets up the local task manager for the test.
    */
diff --git a/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php b/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php
index 8309852..dfaa5d3 100644
--- a/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php
+++ b/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php
@@ -120,15 +120,6 @@ protected function setUp() {
   }
 
   /**
-   * {@inheritdoc}
-   */
-  protected function tearDown() {
-    parent::tearDown();
-    $container = new ContainerBuilder();
-    \Drupal::setContainer($container);
-  }
-
-  /**
    * Tests the has permission method.
    *
    * @param string $permission
diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php
index 958ffde..e869b9f 100644
--- a/core/tests/Drupal/Tests/UnitTestCase.php
+++ b/core/tests/Drupal/Tests/UnitTestCase.php
@@ -8,6 +8,7 @@
 namespace Drupal\Tests;
 
 use Drupal\Component\Utility\Random;
+use Drupal\Core\DependencyInjection\ContainerBuilder;
 
 /**
  * Provides a base class and helpers for Drupal unit tests.
@@ -41,6 +42,17 @@ public static function getInfo() {
   }
 
   /**
+   * {@inheritdoc}
+   */
+  protected function tearDown() {
+    parent::tearDown();
+    if (\Drupal::getContainer()) {
+      $container = new ContainerBuilder();
+      \Drupal::setContainer($container);
+    }
+  }
+
+  /**
    * Generates a unique random string containing letters and numbers.
    *
    * @param int $length
