How to inject a fake class into container when running WebTestBase tests?
Here's the solution.

Let's say your module A defines a new service.

In order to alter that service, you need to create a separate module B and follow the steps outlined in Overriding the default service class section on https://api.drupal.org/api/drupal/core%21core.api.php/group/container/8.

Make sure to list module A as a dependency in module B info.yml file.

Add both modules to the $modules array in your test class, and everything will work. $this-&gt;container-&gt;set() is no longer needed.

That issue on drupal.org: https://www.drupal.org/node/2700739