How to prevent the use of ObjectManagaer in the Unit test Magento 2?

Magento 2 with the Unit test for a module, Avoid direct use of Helper\ObjectManager in a Unit test.

Many developers used the native Magento\Framework\TestFramework\Unit\Helper\ObjectManager class for creating mock and objects.

In the initial launch of Magento 2, They used ObjectManager class to write Unit test due to constructor dependencies are not stable.

Now Magento has a constructor dependency stable and Core Magento reduces the use of Helper ObjectManager directly in Unit test.

Old Pattern,

<?php

use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Action\Context;
use Magento\Contact\Model\ConfigInterface;
use Rbj\TestModule\Controller\Index\Index;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class IndexTest extends TestCase
{
	/**
     * Context Object
     *
     * @var Context|PHPUnit_Framework_MockObject_MockObject
     */
    private $context;

	/**
     * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $configMock;

	protected function setUp(): void
    {
        $objectManager = new ObjectManager($this);

        $this->context = $this->getMockBuilder(Context::class
        )->disableOriginalConstructor()->getMock();

        $this->configMock = $this->getMockBuilder(ConfigInterface::class)->getMockForAbstractClass();

        $this->controller = $objectManager->getObject(
            Index::class,
            [
                'context' => $this->context,
                'configMock' => $this->configMock
            ]
        );
    }

In the above test case, we have used ObjectManager to define the constructor method of the parent class.

New Way without the use of ObjectManager in a Unit test,

<?php
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Action\Context;
use Magento\Contact\Model\ConfigInterface;
use Rbj\TestModule\Controller\Index\Index;

class IndexTest extends TestCase
{
	/**
     * Context Object
     *
     * @var Context|PHPUnit_Framework_MockObject_MockObject
     */
    private $context;

	/**
     * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $configMock;

	protected function setUp(): void
    {
        $this->context = $this->getMockBuilder(Context::class
        )->disableOriginalConstructor()->getMock();

        $this->configMock = $this->getMockBuilder(ConfigInterface::class)->getMockForAbstractClass();

        $this->controller = new Index(
            $this->context,
            $this->configMock
        );
    }

We directly call our Class with the new keyword and create an Object for Main class.