How can I mock a service? i have this code: 

/**
 * Defines a wrapper target around a paragraph bundle's target field.
 *
 * @FeedsTarget(
 *   id = "feeds_para_mapper_temp_target",
 *   field_types = {
 *     "entity_reference_revisions",
 *   },
 *   arguments = {
 *     "@entity.manager",
 *     "@entity_field.manager"
 *   }
 * )
 */
class TempTarget extends FieldTargetBase implements, ConfigurableTargetInterface,
{
  protected $entity_type_manager;
  protected $field_manager;
  public function __construct(array $conf, $id, array $def, EntityTypeManagerInterface $en_man, EntityFieldManagerInterface $f_man)
  {
    parent::__construct($conf, $id, $def);
    $this-&gt;entity_type_manager = $en_man;
    $this-&gt;field_manager = $f_man;
  }
  // Here is where i need the instance, and it's weird because it gets called 
  // without instantiating this class:
  public function defaultConfiguration(){
    // This function create a form configuration and return it:
    $definition = $this-&gt;targetDefinition;
    $field = $definition-&gt;getFieldDefinition();
    $instance = $this-&gt;createTargetInstance($field,$this-&gt;feedType)
  }
  // Calls this plugin manager to get instance of another plugin
  public function createTargetInstance(FieldConfigInterface $field, FeedTypeInterface $feed_type){
    // This is where the test is failing:
    $manager = \Drupal::service('plugin.manager.feeds.target');
  }
}


what it does it calls a plugin manager in order to create instance definition,
whenever it gets tested i see this error:

\Drupal::$container is not initialized yet.


which i know is caused by calling to the service
i have tried this in the test file:

$container = new ContainerBuilder();
$manager = $this-&gt;prophesize('Drupal\feeds\Plugin\Type\FeedsPluginManager');
$container-&gt;set('plugin.manager.feeds.target', $manager);
\Drupal::setContainer($container);


Not working.
i tried as the answer suggested in the comment bellow to add implement ContainerInjectionInterface not working, and with ContainerFactoryPluginInterface as this article suggested, not working too, in both implementations the create() method is never get called
i tried to add this to he annotation:

 *   arguments = {
 *     "@entity.manager",
 *     "@entity_field.manager",
 *     "@plugin.manager.feeds.target"
 *   }


with this constructor:

public function __construct(array $$conf, $id, array $def, EntityTypeManagerInterface $en_m, EntityFieldManagerInterface $f_m, FeedsPluginManager $p_m)
  {
    $stop = null;
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this-&gt;entity_type_manager = $en_m;
    $this-&gt;field_manager = $f_m;
    $this-&gt;plugin_manager = $p_m;
  }


and it's working only if this class is instantiated, and as i said it's not when the defaultCongiguration() get called, i can go without the plugin manager at this stage but i really prefer if i can have it in this function. 

Update:

The reason why implementing either ContainerInjectionInterface or ContainerFactoryPluginInterface didn't work is because i added my dependencies in the arguments, annotation and in the create() method too.
This is probably a unit test (not mentioned in the OP). If so, the intention should be to test the methods of the TempTarget class, which means, that the first thing needed would be to instantiate an instance of that class, e.g.

/**
 * Instantiates and returns a TempTarget object.
 */
protected function getTempTargetInstance()
{
  $id = $this-&gt;generateId();
  $def = $this-&gt;generateDef();
  // This is where you mock the constructor dependencies/services.
  // You probably will need to create a mock like this:
  //   $this-&gt;getMockBuilder('Drupal\Core\Entity\EntityTypeManager')
  //     -&gt;disableOriginalConstructor()
  //     -&gt;getMock();
  $this-&gt;entityTypeManager = $this-&gt;getEntityTypeManagerMock();
  $this-&gt;entityFieldManager = $this-&gt;getEntityFieldManagerMock();

  // You can now call the constructor.
  $tempTarget = new TempTarget($id, $def, $this-&gt;entityTypeManager, $this-&gt;entityFieldManager);
  return $tempTarget;
}


At this point, your object should be instantiated and you can call methods on it to assert the assumptions you want to be testing. For example:

public function testDefaultConfigurationMethod()
{
  $tempTarget = $this-&gt;getTempTargetInstance();
  $formConfiguration = $tempTarget-&gt;defaultConfiguration();
  // Test different assertions given the $formConfiguration object.  
}


However, at the time you call defaultConfiguration() you know that you need a container available that holds an entry for plugin.manager.feeds.target, so, you have to have that prepared in advance. The right place for that would be your setUp() method in your class, for example:

/**
 * {@inheritdoc}
 */
public function setUp() {
  parent::setUp();

  // Provide a mock service container, for the services our module uses.
  $container = new ContainerBuilder();
  $container-&gt;set('plugin.manager.feeds.target', $this-&gt;getFeedsTargetPluginManagerMock());
  \Drupal::setContainer($container);
  ...
  // Further setting-up of your testing class.
}


with getFeedsTargetPluginManagerMock() looking something like this:

/**
 * Creates and returns a mock for the feeds  
 */
protected function getFeedsTargetPluginManagerMock() {
  $feedsTargetPluginManager = $this
    -&gt;getMockBuilder('Drupal\feeds\Plugin\Type\FeedsPluginManager')
    -&gt;disableOriginalConstructor()
    -&gt;getMock();
  ...
  // Further mocking of the objects methods if needed.
  ...
  return $feedsTargetPluginManager;
}


Good luck!