I tried to run a unit but failed with the following error.

My test class


namespace Vendor\Module\Test\Unit\Observer;

class CheckoutOnepageControllerSuccessActionObserverTest extends \PHPUnit_Framework_TestCase {

    public function testExecute() {


I run the phpunit command at /dev/tests/unit/

phpunit --filter CheckoutOnepageControllerSuccessActionObserverTest

It returns the following errors

PHP Fatal error:  Call to protected method PHPUnit_Framework_TestCase::getMockForAbstractClass() from context 'Magento\Framework\TestFramework\Unit\Helper\ObjectManager' in /vendor/magento/framework/TestFramework/Unit/Helper/ObjectManager.php on line 139

Fatal error: Call to protected method PHPUnit_Framework_TestCase::getMockForAbstractClass() from context 'Magento\Framework\TestFramework\Unit\Helper\ObjectManager' in /vendor/magento/framework/TestFramework/Unit/Helper/ObjectManager.php on line 139
  • In order to determine the code that causes the failure the full stack trace is required. The code and the command you posted should not be creating mocks.
    – Vinai
    Sep 11, 2016 at 6:14

2 Answers 2


The error probably is caused by some other test initializing an object with the object manager during construction, which is trying to mock the arguments.
This shouldn't happen unless the tests actually are being executed. Given the code and the command you posted, the error is not on your side.


If you only want to run a single test class, it is much faster to specify the file as a command line argument instead of using the --filter parameter.

When using --filter phpunit will scan all tests for matches. This takes a long time, and along the way the error you encountered is triggered.

Instead, execute phpunit as follows.

From dev/tests/unit:

../../../vendor/bin/phpunit ../../../app/code/Vendor/Module/Test/Unit/Observer/CheckoutOnepageControllerSuccessActionObserverTest.php

Or from the Magento base directory:

vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Vendor/Module/Test/Unit/Observer/CheckoutOnepageControllerSuccessActionObserverTest.php

The phpunit command can also take a directory path instead of a file, which will limit the scanning for tests to that directory branch.
You can still use --filter to limit the test run to a single test method if you need to.


I believe this problem is caused when you try to use a different version of PHPUnit than the one that comes bundled with Magento 2. Magento 2 by default comes with PHPUnit 4.1.0. If you look at the getMock()-method, there's nothing strange there. It's nicely public so it all works as expected.

Link: https://github.com/sebastianbergmann/phpunit/blob/4.1/src/Framework/TestCase.php#L1386


If you decide that you want to use a newer version of PHPUnit (since PHPUnit 4 is not PHP7 compatible) you'll be in for a treat. For example, the 5.7 branch is all of a sudden protected:

Link: https://github.com/sebastianbergmann/phpunit/blob/5.7/src/Framework/TestCase.php#L1738

The answer in this case is to use a PHPUnit version that is PHP7 compatible and still has those critical methods set to public. If I look at the source code, it looks like PHPUnit 5.3 is the version that has these qualities.

Haven't tested it yet, but I'll get back to this topic if that's the answer.

  • it seems the PHPUnit version now in magento is 6.5 What I don't understand is how it can manage all the other phpunit/phpunit in dependencies, I don't know if these errors are coming from a bad upgrade, or magento is behind in something ...
    – medmek
    Mar 29, 2019 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.