From e576927775af3e37923fc0519ebaf31b6ec6564f Mon Sep 17 00:00:00 2001
From: Joan He <johe@magento.com>
Date: Thu, 20 Oct 2016 11:29:57 -0500
Subject: [PATCH] MAGETWO-59876: Eliminate mockObjectManager and
 restoreObjectManager methods in
 \Magento\Framework\TestFramework\Unit\Helper\ObjectManager

---
 .../Magento/Backend/Model/Session/Quote.php   |  7 --
 .../Test/Unit/Model/Session/QuoteTest.php     | 21 ----
 .../ResourceModel/Product/Collection.php      | 20 +++-
 .../Unit/Model/ProductTypes/ConfigTest.php    | 56 +++++++++--
 .../ResourceModel/Product/CollectionTest.php  | 54 +++++++++--
 .../Product/Link/Product/CollectionTest.php   | 34 ++++++-
 .../Product/Option/CollectionTest.php         | 31 +++++-
 .../CatalogRule/Test/Unit/Model/RuleTest.php  | 25 ++++-
 .../ResourceModel/Advanced/CollectionTest.php | 39 +++++++-
 .../ResourceModel/Fulltext/CollectionTest.php | 35 ++++++-
 .../Test/Unit/Model/RuleTest.php              | 25 ++++-
 .../ShippingInformationManagementTest.php     | 26 ++---
 .../Cron/Test/Unit/Model/Config/DataTest.php  | 31 ++++--
 .../Test/Unit/Model/Address/ConfigTest.php    | 31 ++++--
 .../Country/Postcode/Config/DataTest.php      | 33 +++++--
 .../Import/Product/Type/DownloadableTest.php  | 25 ++++-
 .../Model/Entity/Attribute/ConfigTest.php     | 31 ++++--
 .../Test/Unit/Model/Export/ConfigTest.php     | 95 +++++++++++--------
 .../Test/Unit/Model/Import/ConfigTest.php     | 81 +++++++++-------
 .../Test/Unit/Model/Config/DataTest.php       | 31 ++++--
 .../Unit/Model/Quote/Item/RepositoryTest.php  | 13 +--
 .../Model/ShippingAddressManagementTest.php   | 14 +--
 .../Review/Product/CollectionTest.php         | 36 ++++++-
 .../Sales/Test/Unit/Model/Config/DataTest.php | 31 ++++--
 .../Unit/Model/ResourceModel/RuleTest.php     | 25 ++++-
 .../SalesRule/Test/Unit/Model/RuleTest.php    | 25 ++++-
 .../App/Test/Unit/Config/InitialTest.php      | 45 +++++----
 .../App/Test/Unit/Router/ActionListTest.php   | 25 ++++-
 .../Config/Test/Unit/Data/ScopedTest.php      | 27 +++---
 .../Framework/Config/Test/Unit/DataTest.php   | 33 +++++--
 .../Test/Unit/Config/ConfigTest.php           | 25 ++++-
 .../Mview/Test/Unit/Config/DataTest.php       | 33 +++++--
 .../Unit/Helper/ObjectManager.php             | 38 --------
 33 files changed, 781 insertions(+), 320 deletions(-)

diff --git a/app/code/Magento/Backend/Model/Session/Quote.php b/app/code/Magento/Backend/Model/Session/Quote.php
index 12a4d4d138f..6ca26948829 100644
--- a/app/code/Magento/Backend/Model/Session/Quote.php
+++ b/app/code/Magento/Backend/Model/Session/Quote.php
@@ -7,8 +7,6 @@ namespace Magento\Backend\Model\Session;
 
 use Magento\Customer\Api\CustomerRepositoryInterface;
 use Magento\Customer\Api\GroupManagementInterface;
-use Magento\Framework\App\ObjectManager;
-use Magento\Quote\Api\CartManagementInterface;
 
 /**
  * Adminhtml quote session
@@ -81,11 +79,6 @@ class Quote extends \Magento\Framework\Session\SessionManager
      */
     protected $quoteFactory;
 
-    /**
-     * @var \Magento\Quote\Api\CartManagementInterface;
-     */
-    private $cartManagement;
-
     /**
      * @param \Magento\Framework\App\Request\Http $request
      * @param \Magento\Framework\Session\SidResolverInterface $sidResolver
diff --git a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php
index f6ddb41abf5..99d7fdfab21 100644
--- a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php
+++ b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php
@@ -97,11 +97,6 @@ class QuoteTest extends \PHPUnit_Framework_TestCase
      */
     protected $quoteFactoryMock;
 
-    /**
-     * @var \PHPUnit_Framework_MockObject_MockObject
-     */
-    protected $cartManagementMock;
-
     /**
      * Set up
      *
@@ -203,13 +198,6 @@ class QuoteTest extends \PHPUnit_Framework_TestCase
         );
 
         $this->quoteFactoryMock = $this->getMock(\Magento\Quote\Model\QuoteFactory::class, ['create'], [], '', false);
-        $this->cartManagementMock = $this->getMock(
-            \Magento\Quote\Api\CartManagementInterface::class,
-            [],
-            [],
-            '',
-            false
-        );
 
         $this->quote = $this->getMock(
             \Magento\Backend\Model\Session\Quote::class,
@@ -232,15 +220,6 @@ class QuoteTest extends \PHPUnit_Framework_TestCase
                 'quoteFactory' => $this->quoteFactoryMock
             ]
         );
-
-        $this->objectManager->mockObjectManager([
-            \Magento\Quote\Api\CartManagementInterface::class => $this->cartManagementMock
-        ]);
-    }
-
-    protected function tearDown()
-    {
-        $this->objectManager->restoreObjectManager();
     }
 
     /**
diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php
index c24460981af..e4af92baf85 100644
--- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php
+++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php
@@ -260,6 +260,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
      */
     private $metadataPool;
 
+    /**
+     * @var \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory
+     */
+    private $productLimitationFiltersFactory;
+
     /**
      * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
      * @param \Psr\Log\LoggerInterface $logger
@@ -316,7 +321,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
         $this->_resourceHelper = $resourceHelper;
         $this->dateTime = $dateTime;
         $this->_groupManagement = $groupManagement;
-        $this->_productLimitationFilters = $this->createLimitationFilters();
+        $this->_productLimitationFilters = $this->getProductLimitationFactory()->create();
         parent::__construct(
             $entityFactory,
             $logger,
@@ -2336,11 +2341,16 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
     }
 
     /**
-     * @return Collection\ProductLimitation
+     * @deprecated
+     * @return \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory
      */
-    private function createLimitationFilters()
+    private function getProductLimitationFactory()
     {
-        return \Magento\Framework\App\ObjectManager::getInstance()
-                ->create(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class);
+        if (null === $this->productLimitationFiltersFactory) {
+            $this->productLimitationFiltersFactory = ObjectManager::getInstance()->get(
+                \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory::class
+            );
+        }
+        return $this->productLimitationFiltersFactory;
     }
 }
diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php
index 44ef53a34d0..1842b8470c4 100644
--- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php
+++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php
@@ -28,7 +28,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
     private $serializerMock;
 
     /**
-     * @var \Magento\Catalog\Model\ProductTypes\Config|\PHPUnit_Framework_MockObject_MockObject
+     * @var \Magento\Catalog\Model\ProductTypes\Config
      */
     private $config;
 
@@ -44,14 +44,16 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
         );
         $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class);
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
     /**
@@ -67,7 +69,15 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
 
         $this->serializerMock->method('unserialize')
             ->willReturn($value);
-        $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id');
+
+        $this->config = $this->objectManager->getObject(
+            \Magento\Catalog\Model\ProductTypes\Config::class,
+            [
+                'reader' => $this->readerMock,
+                'cache' => $this->cacheMock,
+                'cacheId' => 'cache_id',
+            ]
+        );
         $this->assertEquals($expected, $this->config->getType('global'));
     }
 
@@ -87,7 +97,15 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
             ->willReturn(json_encode('"types":["Expected Data"]]'));
         $this->serializerMock->method('unserialize')
             ->willReturn(['types' => $expected]);
-        $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id');
+
+        $this->config = $this->objectManager->getObject(
+            \Magento\Catalog\Model\ProductTypes\Config::class,
+            [
+                'reader' => $this->readerMock,
+                'cache' => $this->cacheMock,
+                'cacheId' => 'cache_id',
+            ]
+        );
         $this->assertEquals($expected, $this->config->getAll());
     }
 
@@ -98,8 +116,34 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
             ->willReturn('');
         $this->serializerMock->method('unserialize')
             ->willReturn([]);
-        $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id');
 
+        $this->config = $this->objectManager->getObject(
+            \Magento\Catalog\Model\ProductTypes\Config::class,
+            [
+                'reader' => $this->readerMock,
+                'cache' => $this->cacheMock,
+                'cacheId' => 'cache_id',
+            ]
+        );
         $this->assertEquals(false, $this->config->isProductSet('typeId'));
     }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
+    }
 }
diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php
index 5f78d3dba0f..b2ebc56e093 100644
--- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php
+++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php
@@ -5,6 +5,8 @@
  */
 namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product;
 
+use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
+
 /**
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  */
@@ -150,12 +152,14 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
         $productLimitationMock = $this->getMock(
             \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class
         );
-        $this->objectManager->mockObjectManager([
-            \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $productLimitationMock,
-            \Magento\Catalog\Model\ResourceModel\Product\Gallery::class => $this->galleryResourceMock,
-            \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock,
-            \Magento\Catalog\Model\Product\Gallery\ReadHandler::class => $this->galleryReadHandlerMock
-        ]);
+        $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']);
+        $productLimitationFactoryMock->method('create')
+            ->willReturn($productLimitationMock);
+        $this->mockObjectManager(
+            [
+                ProductLimitationFactory::class => $productLimitationFactoryMock,
+            ]
+        );
         $this->collection = $this->objectManager->getObject(
             \Magento\Catalog\Model\ResourceModel\Product\Collection::class,
             [
@@ -182,11 +186,28 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
             ]
         );
         $this->collection->setConnection($this->connectionMock);
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->collection,
+            'mediaGalleryResource',
+            $this->galleryResourceMock
+        );
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->collection,
+            'metadataPool',
+            $this->metadataPoolMock
+        );
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->collection,
+            'productGalleryReadHandler',
+            $this->galleryReadHandlerMock
+        );
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
     public function testAddProductCategoriesFilter()
@@ -255,4 +276,23 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
 
         $this->assertSame($this->collection, $this->collection->addMediaGalleryData());
     }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
+    }
 }
diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php
index 11eb542773d..93e5224387c 100644
--- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php
+++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php
@@ -5,6 +5,9 @@
  */
 namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Link\Product;
 
+use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
+use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation;
+
 /**
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  * @SuppressWarnings(PHPMD.TooManyFields)
@@ -131,10 +134,10 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
         $this->timezoneInterfaceMock = $this->getMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
         $this->sessionMock = $this->getMock(\Magento\Customer\Model\Session::class, [], [], '', false);
         $this->dateTimeMock = $this->getMock(\Magento\Framework\Stdlib\DateTime::class);
-        $this->objectManager->mockObjectManager([
-            \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class =>
-                $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class)
-        ]);
+        $productLimitationFactory = $this->getMock(ProductLimitationFactory::class, ['create']);
+        $productLimitationFactory->method('create')
+            ->willReturn($this->getMock(ProductLimitation::class));
+        $this->mockObjectManager([ProductLimitationFactory::class => $productLimitationFactory]);
 
         $this->collection = $this->objectManager->getObject(
             \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection::class,
@@ -163,7 +166,9 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
     public function testSetProduct()
@@ -176,4 +181,23 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
         $this->collection->setProduct($product);
         $this->assertEquals(33, $this->collection->getStoreId());
     }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
+    }
 }
diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php
index 0aa45ddb7fb..33da480ba1a 100644
--- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php
+++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php
@@ -150,9 +150,8 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
         $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($metadata);
         $this->selectMock->expects($this->exactly(2))->method('join');
 
-        $this->objectManager->mockObjectManager([
+        $this->mockObjectManager([
             \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock,
-            \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class => $this->joinProcessor
         ]);
 
         $this->collection = new Collection(
@@ -165,15 +164,41 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
             null,
             $this->resourceMock
         );
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->collection,
+            'joinProcessor',
+            $this->joinProcessor
+        );
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
     public function testReset()
     {
         $this->collection->reset();
     }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
+    }
 }
diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php
index f9a8ead3abf..22252d218f5 100644
--- a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php
+++ b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php
@@ -124,7 +124,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase
             false
         );
 
-        $this->objectManager->mockObjectManager([
+        $this->mockObjectManager([
             \Magento\Framework\Api\ExtensionAttributesFactory::class =>
                 $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false),
             \Magento\Framework\Api\AttributeValueFactory::class =>
@@ -145,7 +145,9 @@ class RuleTest extends \PHPUnit_Framework_TestCase
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
     /**
@@ -370,4 +372,23 @@ class RuleTest extends \PHPUnit_Framework_TestCase
         $expectedResult = 'form_namerule_conditions_fieldset_100';
         $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName));
     }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
+    }
 }
diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php
index 0a7363d1821..a205d23ee2a 100644
--- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php
+++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php
@@ -7,6 +7,7 @@ namespace Magento\CatalogSearch\Test\Unit\Model\ResourceModel\Advanced;
 
 use Magento\Catalog\Model\Product;
 use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollectionTest;
+use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
 
 /**
  * Tests Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection
@@ -70,10 +71,17 @@ class CollectionTest extends BaseCollectionTest
         );
         $this->search = $this->getMock(\Magento\Search\Api\SearchInterface::class, [], [], '', false);
 
-        $this->objectManager->mockObjectManager([
-            \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class =>
-                $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class)
-        ]);
+        $productLimitationMock = $this->getMock(
+            \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class
+        );
+        $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']);
+        $productLimitationFactoryMock->method('create')
+            ->willReturn($productLimitationMock);
+        $this->mockObjectManager(
+            [
+                ProductLimitationFactory::class => $productLimitationFactoryMock,
+            ]
+        );
 
         $this->advancedCollection = $this->objectManager->getObject(
             \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class,
@@ -91,7 +99,9 @@ class CollectionTest extends BaseCollectionTest
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
     public function testLoadWithFilterNoFilters()
@@ -157,4 +167,23 @@ class CollectionTest extends BaseCollectionTest
             ->getMock();
         return $criteriaBuilder;
     }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
+    }
 }
diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php
index 0b762646cc7..ae820a9d3aa 100644
--- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php
+++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php
@@ -8,6 +8,7 @@ namespace Magento\CatalogSearch\Test\Unit\Model\ResourceModel\Fulltext;
 use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollectionTest;
 use Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory;
 use PHPUnit_Framework_MockObject_MockObject as MockObject;
+use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
 
 /**
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -76,10 +77,15 @@ class CollectionTest extends BaseCollectionTest
         $this->criteriaBuilder = $this->getCriteriaBuilder();
         $this->filterBuilder = $this->getFilterBuilder();
 
-        $this->objectManager->mockObjectManager(
+        $productLimitationMock = $this->getMock(
+            \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class
+        );
+        $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']);
+        $productLimitationFactoryMock->method('create')
+            ->willReturn($productLimitationMock);
+        $this->mockObjectManager(
             [
-                \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class =>
-                    $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class)
+                ProductLimitationFactory::class => $productLimitationFactoryMock,
             ]
         );
 
@@ -114,7 +120,9 @@ class CollectionTest extends BaseCollectionTest
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
     /**
@@ -222,4 +230,23 @@ class CollectionTest extends BaseCollectionTest
             ->getMock();
         return $filter;
     }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
+    }
 }
diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php
index 4a3443b6d28..5312dadf13c 100644
--- a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php
+++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php
@@ -30,7 +30,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase
             ->disableOriginalConstructor()
             ->getMock();
 
-        $this->objectManager->mockObjectManager([
+        $this->mockObjectManager([
             \Magento\Framework\Api\ExtensionAttributesFactory::class =>
                 $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false),
             \Magento\Framework\Api\AttributeValueFactory::class =>
@@ -47,7 +47,9 @@ class RuleTest extends \PHPUnit_Framework_TestCase
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
     public function testGetConditionsInstance()
@@ -64,4 +66,23 @@ class RuleTest extends \PHPUnit_Framework_TestCase
     {
         $this->assertNull($this->rule->getActionsInstance());
     }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
+    }
 }
diff --git a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php
index 7a5438ba17a..c0649bef17f 100644
--- a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php
+++ b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php
@@ -181,12 +181,6 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase
         $this->shippingFactoryMock =
             $this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false);
 
-        $this->objectManager->mockObjectManager([
-            \Magento\Quote\Model\ShippingAssignmentFactory::class => $this->shippingAssignmentFactoryMock,
-            \Magento\Quote\Api\Data\CartExtensionFactory::class => $this->cartExtensionFactoryMock,
-            \Magento\Quote\Model\ShippingFactory::class => $this->shippingFactoryMock,
-        ]);
-
         $this->model = new \Magento\Checkout\Model\ShippingInformationManagement(
             $this->paymentMethodManagementMock,
             $this->paymentDetailsFactoryMock,
@@ -198,11 +192,21 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase
             $this->scopeConfigMock,
             $this->totalsCollectorMock
         );
-    }
-
-    protected function tearDown()
-    {
-        $this->objectManager->restoreObjectManager();
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->model,
+            'shippingAssignmentFactory',
+            $this->shippingAssignmentFactoryMock
+        );
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->model,
+            'cartExtensionFactory',
+            $this->cartExtensionFactoryMock
+        );
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->model,
+            'shippingFactory',
+            $this->shippingFactoryMock
+        );
     }
 
     /**
diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php
index 5eecaa147fc..4e78c15c4f3 100644
--- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php
+++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php
@@ -7,19 +7,30 @@ namespace Magento\Cron\Test\Unit\Model\Config;
 
 class DataTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
-     */
-    private $objectManager;
-
-    protected function setUp()
+    protected function tearDown()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
     }
 
-    protected function tearDown()
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
     {
-        $this->objectManager->restoreObjectManager();
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     /**
@@ -53,7 +64,7 @@ class DataTest extends \PHPUnit_Framework_TestCase
         $dbReader->expects($this->once())->method('get')->will($this->returnValue($dbReaderData));
 
         $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock]
         );
 
diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php
index 40a754f78ec..d9499812cea 100644
--- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php
+++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php
@@ -7,11 +7,6 @@ namespace Magento\Customer\Test\Unit\Model\Address;
 
 class ConfigTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
-     */
-    private $objectManager;
-
     /**
      * @var \PHPUnit_Framework_MockObject_MockObject
      */
@@ -59,7 +54,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->_storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
         $this->_scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
 
@@ -104,7 +98,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
             );
 
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
 
@@ -125,7 +119,28 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     public function testGetStore()
diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php
index 2ae230da35c..4581c088e4b 100644
--- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php
+++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php
@@ -7,11 +7,6 @@ namespace Magento\Directory\Test\Unit\Model\Country\Postcode\Config;
 
 class DataTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
-     */
-    private $objectManager;
-
     /**
      * @var \Magento\Directory\Model\Country\Postcode\Config\Reader|\PHPUnit_Framework_MockObject_MockObject
      */
@@ -29,7 +24,6 @@ class DataTest extends \PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->readerMock = $this->getMock(
             \Magento\Directory\Model\Country\Postcode\Config\Reader::class,
             [],
@@ -45,7 +39,7 @@ class DataTest extends \PHPUnit_Framework_TestCase
             false
         );
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
     }
@@ -63,8 +57,29 @@ class DataTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($expected, $configData->get());
     }
 
-    public function tearDown()
+    protected function tearDown()
+    {
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
     {
-        $this->objectManager->restoreObjectManager();
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 }
diff --git a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php
index f3d5d4f5a9d..592be58c9b4 100644
--- a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php
+++ b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php
@@ -710,7 +710,7 @@ class DownloadableTest extends \Magento\ImportExport\Test\Unit\Model\Import\Abst
             ->getMock(\Magento\Framework\EntityManager\MetadataPool::class, ['getLinkField'], [], '', false);
         $metadataPoolMock->expects($this->any())->method('getMetadata')->willReturnSelf();
 
-        $this->objectManagerHelper->mockObjectManager([
+        $this->mockObjectManager([
             \Magento\Framework\EntityManager\MetadataPool::class => $metadataPoolMock
         ]);
 
@@ -739,7 +739,28 @@ class DownloadableTest extends \Magento\ImportExport\Test\Unit\Model\Import\Abst
     protected function tearDown()
     {
         parent::tearDown();
-        $this->objectManagerHelper->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     /**
diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php
index b111bf8c72a..90ac040af5b 100644
--- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php
+++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php
@@ -11,11 +11,6 @@ namespace Magento\Eav\Test\Unit\Model\Entity\Attribute;
 
 class ConfigTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
-     */
-    private $objectManager;
-
     /**
      * @var \Magento\Eav\Model\Entity\Attribute\Config
      */
@@ -48,7 +43,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->_attribute = $this->getMock(\Magento\Eav\Model\Entity\Attribute::class, [], [], '', false);
         $this->_entityType = $this->getMock(\Magento\Eav\Model\Entity\Type::class, [], [], '', false);
         $this->_readerMock = $this->getMock(
@@ -69,7 +63,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
 
         $serializerMock->method('unserialize')
             ->willReturn([]);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock]
         );
         $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config(
@@ -81,7 +75,28 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     public function testGetLockedFieldsEmpty()
diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php
index 04cb6bbcab6..d00642d86e9 100644
--- a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php
+++ b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php
@@ -8,19 +8,14 @@ namespace Magento\ImportExport\Test\Unit\Model\Export;
 class ConfigTest extends \PHPUnit_Framework_TestCase
 {
     /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
+     * @var \Magento\ImportExport\Model\Export\Config\Reader|\PHPUnit_Framework_MockObject_MockObject
      */
-    private $objectManager;
+    protected $readerMock;
 
     /**
-     * @var \PHPUnit_Framework_MockObject_MockObject
+     * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
      */
-    protected $_readerMock;
-
-    /**
-     * @var \PHPUnit_Framework_MockObject_MockObject
-     */
-    protected $_configScopeMock;
+    protected $cacheMock;
 
     /**
      * @var \Magento\Framework\Serialize\SerializerInterface
@@ -30,33 +25,53 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
     /**
      * @var string
      */
-    protected $_cacheId = 'some_id';
+    protected $cacheId = 'some_id';
 
     /**
      * @var \Magento\ImportExport\Model\Export\Config
      */
-    protected $_model;
+    protected $model;
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
-        $this->_readerMock = $this->getMock(
+        $this->readerMock = $this->getMock(
             \Magento\ImportExport\Model\Export\Config\Reader::class,
             [],
             [],
             '',
             false
         );
-        $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class);
+        $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class);
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     /**
@@ -66,22 +81,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
      */
     public function testGetEntities($value, $expected)
     {
-        $this->_configScopeMock->expects(
+        $this->cacheMock->expects(
             $this->any()
         )->method(
             'load'
         )->with(
-            $this->_cacheId
+            $this->cacheId
         )->will(
             $this->returnValue(false)
         );
-        $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value));
-        $this->_model = new \Magento\ImportExport\Model\Export\Config(
-            $this->_readerMock,
-            $this->_configScopeMock,
-            $this->_cacheId
+        $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value));
+        $this->model = new \Magento\ImportExport\Model\Export\Config(
+            $this->readerMock,
+            $this->cacheMock,
+            $this->cacheId
         );
-        $this->assertEquals($expected, $this->_model->getEntities('entities'));
+        $this->assertEquals($expected, $this->model->getEntities('entities'));
     }
 
     public function getEntitiesDataProvider()
@@ -100,22 +115,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
      */
     public function testGetEntityTypes($configData, $entity, $expectedResult)
     {
-        $this->_configScopeMock->expects(
+        $this->cacheMock->expects(
             $this->any()
         )->method(
             'load'
         )->with(
-            $this->_cacheId
+            $this->cacheId
         )->will(
             $this->returnValue(false)
         );
-        $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($configData));
-        $this->_model = new \Magento\ImportExport\Model\Export\Config(
-            $this->_readerMock,
-            $this->_configScopeMock,
-            $this->_cacheId
+        $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($configData));
+        $this->model = new \Magento\ImportExport\Model\Export\Config(
+            $this->readerMock,
+            $this->cacheMock,
+            $this->cacheId
         );
-        $this->assertEquals($expectedResult, $this->_model->getEntityTypes($entity));
+        $this->assertEquals($expectedResult, $this->model->getEntityTypes($entity));
     }
 
     public function getEntityTypesDataProvider()
@@ -153,22 +168,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
      */
     public function testGetFileFormats($value, $expected)
     {
-        $this->_configScopeMock->expects(
+        $this->cacheMock->expects(
             $this->any()
         )->method(
             'load'
         )->with(
-            $this->_cacheId
+            $this->cacheId
         )->will(
             $this->returnValue(false)
         );
-        $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value));
-        $this->_model = new \Magento\ImportExport\Model\Export\Config(
-            $this->_readerMock,
-            $this->_configScopeMock,
-            $this->_cacheId
+        $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value));
+        $this->model = new \Magento\ImportExport\Model\Export\Config(
+            $this->readerMock,
+            $this->cacheMock,
+            $this->cacheId
         );
-        $this->assertEquals($expected, $this->_model->getFileFormats('fileFormats'));
+        $this->assertEquals($expected, $this->model->getFileFormats('fileFormats'));
     }
 
     public function getFileFormatsDataProvider()
diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php
index 2e182c4e355..c3f9f6c81cf 100644
--- a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php
+++ b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php
@@ -8,19 +8,14 @@ namespace Magento\ImportExport\Test\Unit\Model\Import;
 class ConfigTest extends \PHPUnit_Framework_TestCase
 {
     /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
+     * @var \Magento\ImportExport\Model\Import\Config\Reader|\PHPUnit_Framework_MockObject_MockObject
      */
-    private $objectManager;
+    protected $readerMock;
 
     /**
-     * @var \PHPUnit_Framework_MockObject_MockObject
+     * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
      */
-    protected $_readerMock;
-
-    /**
-     * @var \PHPUnit_Framework_MockObject_MockObject
-     */
-    protected $_configScopeMock;
+    protected $cacheMock;
 
     /**
      * @var \Magento\Framework\Serialize\SerializerInterface
@@ -30,33 +25,53 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
     /**
      * @var string
      */
-    protected $_cacheId = 'some_id';
+    protected $cacheId = 'some_id';
 
     /**
      * @var \Magento\ImportExport\Model\Import\Config
      */
-    protected $_model;
+    protected $model;
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
-        $this->_readerMock = $this->getMock(
+        $this->readerMock = $this->getMock(
             \Magento\ImportExport\Model\Import\Config\Reader::class,
             [],
             [],
             '',
             false
         );
-        $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class);
+        $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class);
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
     }
 
-    public function tearDown()
+    protected function tearDown()
+    {
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
     {
-        $this->objectManager->restoreObjectManager();
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     /**
@@ -66,22 +81,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
      */
     public function testGetEntities($value, $expected)
     {
-        $this->_configScopeMock->expects(
+        $this->cacheMock->expects(
             $this->any()
         )->method(
             'load'
         )->with(
-            $this->_cacheId
+            $this->cacheId
         )->will(
             $this->returnValue(false)
         );
-        $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value));
-        $this->_model = new \Magento\ImportExport\Model\Import\Config(
-            $this->_readerMock,
-            $this->_configScopeMock,
-            $this->_cacheId
+        $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value));
+        $this->model = new \Magento\ImportExport\Model\Import\Config(
+            $this->readerMock,
+            $this->cacheMock,
+            $this->cacheId
         );
-        $this->assertEquals($expected, $this->_model->getEntities('entities'));
+        $this->assertEquals($expected, $this->model->getEntities('entities'));
     }
 
     public function getEntitiesDataProvider()
@@ -100,22 +115,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
      */
     public function testGetEntityTypes($configData, $entity, $expectedResult)
     {
-        $this->_configScopeMock->expects(
+        $this->cacheMock->expects(
             $this->any()
         )->method(
             'load'
         )->with(
-            $this->_cacheId
+            $this->cacheId
         )->will(
             $this->returnValue(false)
         );
-        $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($configData));
-        $this->_model = new \Magento\ImportExport\Model\Import\Config(
-            $this->_readerMock,
-            $this->_configScopeMock,
-            $this->_cacheId
+        $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($configData));
+        $this->model = new \Magento\ImportExport\Model\Import\Config(
+            $this->readerMock,
+            $this->cacheMock,
+            $this->cacheId
         );
-        $this->assertEquals($expectedResult, $this->_model->getEntityTypes($entity));
+        $this->assertEquals($expectedResult, $this->model->getEntityTypes($entity));
     }
 
     public function getEntityTypesDataProvider()
diff --git a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php
index 6747025eed2..9096bb8af1b 100644
--- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php
+++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php
@@ -7,11 +7,6 @@ namespace Magento\Indexer\Test\Unit\Model\Config;
 
 class DataTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
-     */
-    private $objectManager;
-
     /**
      * @var \Magento\Indexer\Model\Config\Data
      */
@@ -49,7 +44,6 @@ class DataTest extends \PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->reader = $this->getMock(\Magento\Framework\Indexer\Config\Reader::class, ['read'], [], '', false);
         $this->cache = $this->getMockForAbstractClass(
             \Magento\Framework\Config\CacheInterface::class,
@@ -68,14 +62,35 @@ class DataTest extends \PHPUnit_Framework_TestCase
             false
         );
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     public function testConstructorWithCache()
diff --git a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php
index 0929cc16fb3..c6e5241282c 100644
--- a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php
+++ b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php
@@ -106,9 +106,6 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase
             '',
             false
         );
-        $this->objectManager->mockObjectManager([
-            \Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor::class => $this->optionsProcessorMock
-        ]);
 
         $this->repository = new \Magento\Quote\Model\Quote\Item\Repository(
             $this->quoteRepositoryMock,
@@ -116,11 +113,11 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase
             $this->itemDataFactoryMock,
             ['custom_options' => $this->customOptionProcessor]
         );
-    }
-
-    protected function tearDown()
-    {
-        $this->objectManager->restoreObjectManager();
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->repository,
+            'cartItemOptionsProcessor',
+            $this->optionsProcessorMock
+        );
     }
 
     /**
diff --git a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php
index 0b820cdfca0..d8e65de8474 100644
--- a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php
+++ b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php
@@ -104,10 +104,6 @@ class ShippingAddressManagementTest extends \PHPUnit_Framework_TestCase
             '',
             false
         );
-        $this->objectManager->mockObjectManager([
-            \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class
-                => $this->amountErrorMessageMock
-        ]);
 
         $this->service = $this->objectManager->getObject(
             \Magento\Quote\Model\ShippingAddressManagement::class,
@@ -120,11 +116,11 @@ class ShippingAddressManagementTest extends \PHPUnit_Framework_TestCase
                 'addressRepository' => $this->addressRepository
             ]
         );
-    }
-
-    protected function tearDown()
-    {
-        $this->objectManager->restoreObjectManager();
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->service,
+            'minimumAmountErrorMessage',
+            $this->amountErrorMessageMock
+        );
     }
 
     /**
diff --git a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php
index 4a61406e8a1..d2a959e0a3a 100644
--- a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php
+++ b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php
@@ -5,6 +5,8 @@
  */
 namespace Magento\Review\Test\Unit\Model\ResourceModel\Review\Product;
 
+use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
+
 /**
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  */
@@ -82,10 +84,15 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
         $productLimitationMock = $this->getMock(
             \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class
         );
+        $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']);
+        $productLimitationFactoryMock->method('create')
+            ->willReturn($productLimitationMock);
+        $this->mockObjectManager(
+            [
+                ProductLimitationFactory::class => $productLimitationFactoryMock,
+            ]
+        );
         $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
-        $this->objectManager->mockObjectManager([
-            \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $productLimitationMock
-        ]);
         $this->model = $this->objectManager->getObject(
             \Magento\Review\Model\ResourceModel\Review\Product\Collection::class,
             [
@@ -99,7 +106,28 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     /**
diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php
index ed001a14c99..cfb2f63c99f 100644
--- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php
+++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php
@@ -7,11 +7,6 @@ namespace Magento\Sales\Test\Unit\Model\Config;
 
 class DataTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
-     */
-    private $objectManager;
-
     /**
      * @var \PHPUnit_Framework_MockObject_MockObject
      */
@@ -29,7 +24,6 @@ class DataTest extends \PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->_readerMock = $this->getMockBuilder(
             \Magento\Sales\Model\Config\Reader::class
         )->disableOriginalConstructor()->getMock();
@@ -37,14 +31,35 @@ class DataTest extends \PHPUnit_Framework_TestCase
             \Magento\Framework\App\Cache\Type\Config::class
         )->disableOriginalConstructor()->getMock();
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     public function testGet()
diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php
index b492d59f568..c0de06df830 100644
--- a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php
+++ b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php
@@ -139,7 +139,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase
                 ]
             );
 
-        $this->objectManager->mockObjectManager([
+        $this->mockObjectManager([
             \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class => $associatedEntitiesMap
         ]);
 
@@ -155,7 +155,28 @@ class RuleTest extends \PHPUnit_Framework_TestCase
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     /**
diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php
index adf98a1528c..d7f58dce737 100644
--- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php
+++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php
@@ -61,7 +61,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase
             ->setMethods(['create'])
             ->getMock();
 
-        $this->objectManager->mockObjectManager([
+        $this->mockObjectManager([
             \Magento\Framework\Api\ExtensionAttributesFactory::class =>
                 $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false),
             \Magento\Framework\Api\AttributeValueFactory::class =>
@@ -80,7 +80,28 @@ class RuleTest extends \PHPUnit_Framework_TestCase
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     public function testLoadCouponCode()
diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php
index ab200ea88ef..2d7f61da267 100644
--- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php
+++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php
@@ -17,11 +17,6 @@ class InitialTest extends \PHPUnit_Framework_TestCase
      */
     private $config;
 
-    /**
-     * @var \Magento\Framework\App\Config\Initial\Reader|\PHPUnit_Framework_MockObject_MockObject
-     */
-    private $readerMock;
-
     /**
      * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject
      */
@@ -42,13 +37,6 @@ class InitialTest extends \PHPUnit_Framework_TestCase
     protected function setUp()
     {
         $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
-        $this->readerMock = $this->getMock(
-            \Magento\Framework\App\Config\Initial\Reader::class,
-            [],
-            [],
-            '',
-            false
-        );
         $this->cacheMock = $this->getMock(
             \Magento\Framework\App\Cache\Type\Config::class,
             [],
@@ -64,19 +52,42 @@ class InitialTest extends \PHPUnit_Framework_TestCase
         $serializerMock->method('unserialize')
             ->willReturn($this->data);
 
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock]
         );
 
-        $this->config = new \Magento\Framework\App\Config\Initial(
-            $this->readerMock,
-            $this->cacheMock
+        $this->config = $this->objectManager->getObject(
+            \Magento\Framework\App\Config\Initial::class,
+            [
+                'cache' => $this->cacheMock,
+            ]
         );
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     /**
diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php
index 993faa1dcd6..744e293029f 100644
--- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php
+++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php
@@ -50,14 +50,35 @@ class ActionListTest extends \PHPUnit_Framework_TestCase
             false
         );
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     public function testConstructActionsCached()
diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php
index 8af81fe5dc6..2b0a677b518 100644
--- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php
+++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php
@@ -43,23 +43,22 @@ class ScopedTest extends \PHPUnit_Framework_TestCase
         $this->_readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class);
         $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class);
         $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class);
-
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
-            [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
-        );
 
-        $this->_model = new \Magento\Framework\Config\Data\Scoped(
-            $this->_readerMock,
-            $this->_configScopeMock,
-            $this->_cacheMock,
-            'tag'
+        $this->_model = $this->objectManager->getObject(
+            \Magento\Framework\Config\Data\Scoped::class,
+            [
+                'reader' => $this->_readerMock,
+                'configScope' => $this->_configScopeMock,
+                'cache' => $this->_cacheMock,
+                'cacheId' => 'tag',
+            ]
+        );
+        $this->objectManager->setBackwardCompatibleProperty(
+            $this->_model,
+            'serializer',
+            $this->serializerMock
         );
-    }
-
-    protected function tearDown()
-    {
-        $this->objectManager->restoreObjectManager();
     }
 
     /**
diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php
index b0425771338..d527f1b0da4 100644
--- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php
+++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php
@@ -10,12 +10,7 @@ namespace Magento\Framework\Config\Test\Unit;
 
 class DataTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
-     */
-    private $objectManager;
-
-    /**
+   /**
      * @var \Magento\Framework\Config\ReaderInterface|\PHPUnit_Framework_MockObject_MockObject
      */
     private $readerMock;
@@ -32,16 +27,36 @@ class DataTest extends \PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class);
         $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class);
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager([\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]);
+        $this->mockObjectManager([\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]);
     }
 
     protected function tearDown()
     {
-        $this->objectManager->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     public function testGetConfigNotCached()
diff --git a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php
index db0218d00af..7ff809810cc 100644
--- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php
+++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php
@@ -77,12 +77,33 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
         );
         $this->serializerMock = $this->getMock(SerializerInterface::class);
         $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
-        $this->objectManagerHelper->mockObjectManager([SerializerInterface::class => $this->serializerMock]);
+        $this->mockObjectManager([SerializerInterface::class => $this->serializerMock]);
     }
 
     protected function tearDown()
     {
-        $this->objectManagerHelper->restoreObjectManager();
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
+    {
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     /**
diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php
index ce9a97b7ac9..8b8b3e737fe 100644
--- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php
+++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php
@@ -7,11 +7,6 @@ namespace Magento\Framework\Mview\Test\Unit\Config;
 
 class DataTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
-     */
-    private $objectManager;
-
     /**
      * @var \Magento\Framework\Mview\Config\Data
      */
@@ -49,7 +44,6 @@ class DataTest extends \PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->reader = $this->getMock(\Magento\Framework\Mview\Config\Reader::class, ['read'], [], '', false);
         $this->cache = $this->getMockForAbstractClass(
             \Magento\Framework\Config\CacheInterface::class,
@@ -71,14 +65,35 @@ class DataTest extends \PHPUnit_Framework_TestCase
         );
 
         $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
-        $this->objectManager->mockObjectManager(
+        $this->mockObjectManager(
             [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]
         );
     }
 
-    public function tearDown()
+    protected function tearDown()
+    {
+        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue(null);
+    }
+
+    /**
+     * Mock application object manager to return configured dependencies.
+     *
+     * @param array $dependencies
+     * @return void
+     */
+    private function mockObjectManager($dependencies)
     {
-        $this->objectManager->restoreObjectManager();
+        $dependencyMap = [];
+        foreach ($dependencies as $type => $instance) {
+            $dependencyMap[] = [$type, $instance];
+        }
+        $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
+        $objectManagerMock->expects($this->any())
+            ->method('get')
+            ->will($this->returnValueMap($dependencyMap));
+        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
     }
 
     public function testConstructorWithCache()
diff --git a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php
index 91ecf842474..16493899d7d 100644
--- a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php
+++ b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php
@@ -342,42 +342,4 @@ class ObjectManager
         $reflectionProperty->setAccessible(true);
         $reflectionProperty->setValue($object, $propertyValue);
     }
-
-    /**
-     * Mock application object manager to return configured dependencies. Can be used in the case when need to add a
-     * new dependency that is used in the constructor keeping backwards compatibility
-     *
-     * $dependencies = [\Magento\Framework\Json\JsonInterface::class => $jsonMock]
-     *
-     * @param array $dependencies
-     * @return void
-     */
-    public function mockObjectManager($dependencies)
-    {
-        $dependencyMap = [];
-        foreach ($dependencies as $type => $instance) {
-            $dependencyMap[] = [$type, $instance];
-        }
-        $objectManagerMock = $this->_testObject->getMock(\Magento\Framework\ObjectManagerInterface::class);
-        $objectManagerMock->expects($this->_testObject->any())
-            ->method('getInstance')
-            ->willReturnSelf();
-        $objectManagerMock->expects($this->_testObject->any())
-            ->method('get')
-            ->will($this->_testObject->returnValueMap($dependencyMap));
-        \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
-    }
-
-    /**
-     * Unset mocked object manager, must be used to restore
-     * \Magento\Framework\App\ObjectManager::_instance after mockObjectManager called
-     *
-     * @return void
-     */
-    public function restoreObjectManager()
-    {
-        $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
-        $reflectionProperty->setAccessible(true);
-        $reflectionProperty->setValue(null);
-    }
 }
-- 
GitLab