PHPUnit testing a method that changes an entity field Let's say I have an entity with certain fields and a method that does the following
public function changeEntityField(Entity $entity) {
  $entity-&gt;set('field_xyz', 20);
  $entity-&gt;save();
  return $entity;
}

I know how to mock an entity and set its field values via $entity-&gt;expects($this-&gt;any())-&gt;method('get')-&gt;with('field_xyz')-&gt;willReturn(10) etc. and pass it to the function to be tested.
What I don't get yet is how to test the function does really set the field_xyz to 20 ?
I tried
$entity = $this-&gt;getMockBuilder(Application::class)
  -&gt;disableOriginalConstructor()
  -&gt;getMock();
$field = $this-&gt;getMockBuilder(FieldItemListInterface::class)
      -&gt;disableOriginalConstructor()
      -&gt;getMock();
$field-&gt;expects($this-&gt;any())
      -&gt;method('__get')
      -&gt;with('value')
      -&gt;willReturn(10);
$entity-&gt;expects($this-&gt;any())
  -&gt;method('get')
  -&gt;with('field_xyz')
  -&gt;willReturn($field);
$result = $this-&gt;service-&gt;changeEntityField($entity);
$this-&gt;assertEqual($result-&gt;get('field_xyz')-&gt;value, 20);

but it returns 10. How do I mock the -&gt;set() methods of the entity? Or do I have to work with real entities and not mocks in such cases?
You can't do that in a unit test, you have to work with real entities in a functional test since $entity-&gt;save(); needs the database to be loaded.
And as $result = $this-&gt;service-&gt;changeEntityField($entity); will return an entity there's no way $result can be equal to 10 or 20. It will be equal to an entity. But in a functional test you'll be able to load the entity and check if the field has the good value or not after your function has been called.