You can use Mockito for this Answer.
doAnswer(new Answer<Object>(){
@Override
public Object answer(InvocationOnMock invocation){
Article article = (Article) invocation.getArguments()[0];
article.setId(1L);
return null;
}
}).when(em).persist(any(Article.class));
This tells Mockito that when calling a method, the persistfirst argument must have its own method setId.
, , . , Mockito Answer, Article EntityManager.