0

I want to mock profileRepository.save() from the method:

public ProfileEty saveProfile(){
    UserEty userEty = new UserEty();
    ProfileEty profile = new ProfileEty();
    SettingsEty settingsEty = new SettingsEty();
    CustomizeEty customizeEty = new CustomizeEty();
    profile.setUser(userEty);
    profile = profileRepository.save(profile);
    return profile;
}

If I try this in my Test

@Test
public void should_saveProfile(){
    //Given
   when(profileRepository.save(SOME_PROFILE)).thenReturn(SOME_PROFILE);
}

Where SOME_PROFILE is equals to:

public static final  ProfileEty SOME_PROFILE = new ProfileEty();

The Profile returned from the mock profileRepository won´t be the same as the one created in the method saveProfile()

How to make sure that I am working with the same Profile, or how to perform this kind of cases?

marhg
  • 659
  • 1
  • 17
  • 30
  • Andreahg, please comment here if you don't think your question is the same as the one I've marked it as a duplicate of, and I'll reopen. – Dawood ibn Kareem Jun 06 '18 at 10:35
  • Please show a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – VijayD Jun 06 '18 at 10:35
  • @DawoodibnKareem, I am sure that it is duplicated, but I can´t find the right source/example or keyword to see how it can be done – marhg Jun 06 '18 at 10:42
  • Did you read the answers on the question that I marked it as a duplicate of? – Dawood ibn Kareem Jun 06 '18 at 10:42
  • yes I looked at it, which post is the one describing the solution?, please – marhg Jun 06 '18 at 10:45
  • All of them. You're asking how to stub your `save` method, so that it just returns the same object that was passed into it, right? That's exactly what Abhijeet was asking how to do. – Dawood ibn Kareem Jun 06 '18 at 10:52
  • you can probabbly do something like ProfileEty ety = PowerMockito.mock(ProfileEty.class) PowerMocito.whenNew(ProfileEty .class).withNoArguments().thenReturn(ety ) – Amit Kumar Lal Jun 06 '18 at 11:10
  • @amRika alright! Thank you for the advice. I will try it out – marhg Jun 06 '18 at 12:14

0 Answers0