Injecting configuration into mock config object I'm trying to set up a unit test based on UnitTestCase in Drupal 8 that uses some config variables. Unfortunately I was not able to find a way to insert those config variables into the system in the setup (searched Drupal documentation and any Drupal unit test example involving config), so the function being tested always works with null config and fails. I am trying to test individual functions in isolation, so a browser test is not really appropriate for this.

I have simplified the problem to the minimum code necessary to explain/reproduce the issue. Here are the three components. Where in the mock/setup do I insert the array('bucketName' =&gt; 'test_bucket') and other properties so that it can be read from the tested function?

Function:

public function example() {
    return \Drupal::config('my_module.job')-&gt;get('my_module.bucketName');
}


Test function:

public function testExample() {
    $myModuleHelper = new MyModuleHelper();
        $expected_config = array(
            'bucketName' =&gt; 'test_bucket',
        );

    $this-&gt;assertEquals($expected_config, $myModuleHelper-&gt;example());
}


Test setup:

public function setUp() {
    $this-&gt;config = $this-&gt;getMockBuilder('\Drupal\Core\Config\ImmutableConfig')
        -&gt;disableOriginalConstructor()
        -&gt;getMock();

    $this-&gt;configFactory = $this-&gt;getMockBuilder('\Drupal\Core\Config\ConfigFactory')
        -&gt;disableOriginalConstructor()
        -&gt;getMock();
    $this-&gt;configFactory-&gt;expects($this-&gt;any())
        -&gt;method('get')
        -&gt;with('my_module.job')
        -&gt;willReturn($this-&gt;config);

    $this-&gt;container = new ContainerBuilder();
    $this-&gt;container-&gt;set('config.factory', $this-&gt;configFactory);
    \Drupal::setContainer($this-&gt;container);
}


Test response:


  null does not match expected type "array".
Your mock of the Config object needs to also have a ::get method and return the appropriate value using your array. The core UnitTestCase class provides ::getConfigFactoryStub(array $configs), which does all of this automatically.
In your setup, create the config factory like this:
$this-&gt;configFactory = $this-&gt;getConfigFactoryStub([
  'mymodule.job' =&gt; ['bucketName' =&gt; 'test_bucket'],
]);

Unfortunately, the mocked config does not quite work like a real config object; namely, it doesn't accept dotted keys but only top-level keys. The following will not work:
$this-&gt;configFactory = $this-&gt;getConfigFactoryStub([
  'mymodule.settings' =&gt; [
    'key' =&gt; [
      'subkey' =&gt; [
        'subsubkey' =&gt; 'value',
      ],
    ],
  ],
]);
$this-&gt;container = new ContainerBuilder();
$this-&gt;container-&gt;set('config.factory', $this-&gt;configFactory);
\Drupal::setContainer($this-&gt;container);

\Drupal::config('mymodule.settings')-&gt;get('key.subkey.subsubkey');

This bug report shows a workaround: https://www.drupal.org/node/2862248
Update: The above problem was fixed in Drupal 8.5.0, so the workaround is no longer needed.