This my the code, and it's working normally, but in test unit, it is not working ? he said that he doesn't have the create method.

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $userModel = $objectManager->create('Magento\User\Model\User');
    $role_id_under_selected_user = $userModel->loadByUsername("bbbb")->getRoles();      
    if ($userModel->getData('password') == NULL || empty($role_id_under_selected_user)) {
        return false;
    } else {
        return true;

Thanks for your help.

  • I tried to use this $objectManager = new ObjectManager($this); instead of $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); but it's didn't works too. – MEIR Feb 19 '17 at 15:06
  • Thanks, but the question is ? Can I check if "bbbb" user is existed or not ? without objectManager... because objectManager is not working for me... – MEIR Feb 21 '17 at 14:53

Since you do not show the unit test code, I can only speculate, but it looks like you have an instance of the unit test object manager \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - it works differently and does not have a create() method. Instead it has (amongst others) a methods getObject() which gives you a new instance of the requested class with all dependencies mocked

It's recommended to neither use the unit object manager nor the real object manager in unit tests!1

Here's a statement from a Magento 2 core developer:

When M2 development started all classes use Mage::* methods and we start increase coverage with unit test. But when we add new constructor injection many test start fails. So, we decide to introduce helper class to avoid the unneeded work.

But now constructor dependencies are stabilized, so helper is not need in most cases

Source: https://magento.stackexchange.com/a/140433/243

Instead, instantiate the object under test with new and instantiate or stub/mock all dependencies explicitly.

In the production code (i.e. not the test), you should not use the object manager either, but instead use dependency injection and factories. If your code needs to instantiate a new User object, add \Magento\User\Api\Data\UserInterfaceFactory $userFactory as constructor parameter, and then replace

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$userModel = $objectManager->create('Magento\User\Model\User');


$userModel = $this->userFactory->create();

If you update the question with test code and maybe some more context, I can give you an example for the test as well.

1) Integration tests are a different topic, there you should use the real object manager to test that your DI configuration works correctly.


From the question title, I guess you actually are writing an integration test, i.e. during the test your code operates on the database and you want to check if a user has been saved as expected.

In that case, the following works perfectly fine:

  1. Get object manager from test framework bootstrap:

    $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  2. Get object manager directly:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

But your test should be executed as integration test, not as unit test. More on that here: Creating Integration Tests for Magento 2 Modules

  • Thanks @Fabian Schmengler, It's still doesn't works for me :/, May you write me a simple and short code for it.. with the constructor and the setup function and the testFunction ... 20-40 line codes how much it's takes.. my code is very simple, and from the begining... I will be more than happy that you will help me and other which stuck with this problem... – MEIR Feb 21 '17 at 10:09
  • class bbbTest extends \PHPUnit_Framework_TestCase this is my class for example... – MEIR Feb 21 '17 at 10:12
  • No, but if you update your question with a simple and short test code to reproduce your issue (minimum working example), I am willing to help fix it – Fabian Schmengler Feb 21 '17 at 11:26
  • I add code in the end... May you help me.. Thanks.. – MEIR Feb 21 '17 at 13:14

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