Issue
I have a builder class written in Java that I would like to test with Mockito.
Profile.java
@Data
@Document
public class Profile {
public final String birthDate;
public final City city;
public final Country country;
public final String imageId;
public final Team team;
public Profile(String birthDate,
City city,
Country country,
String imageId,
Team team) {
this.birthDate = birthDate;
this.city = city;
this.country = country;
this.imageId = imageId;
this.team = team;
}
public static ProfileBuilder builder() {
return new ProfileBuilder();
}
public static final class ProfileBuilder {
public String birthDate;
public City city;
public Country country;
public String imageId;
public Team team;
public ProfileBuilder() {
}
public ProfileBuilder withBirthDate(String birthDate) {
this.birthDate = birthDate;
return this;
}
public ProfileBuilder withCity(City city) {
this.city = city;
return this;
}
public ProfileBuilder withCountry(Country country) {
this.country = country;
return this;
}
public ProfileBuilder withImageId(String imageId) {
this.imageId = imageId;
return this;
}
public ProfileBuilder withTeam(Team team) {
this.team = team;
return this;
}
public Profile build(){
return new Profile(birthDate, city, country, imageId, team);
}
}
}
And I have this method to add Profile to database
@Override
public Profile addProfile(Profile profile) {
Profile createdProfile = Profile.builder()
.withBirthDate(profile.getBirthDate())
.withCity(profile.getCity())
.withCountry(profile.getCountry())
.withTeam(profile.getTeam())
.withImageId(profile.getImageId())
.build();
return profileRepository.save(createdProfile);
}
I am trying to test it like this:
public class ProfileServiceImplTest {
ProfileRepository profileRepository = Mockito.mock(ProfileRepository.class);
private final ProfileServiceImpl profileService = new ProfileServiceImpl(profileRepository);
City city = Mockito.mock(City.class);
Country country = Mockito.mock(Country.class);
Team team = Mockito.mock(Team.class);
@Test
public void addProfileTest(){
Profile profile = new Profile("25.07.1996", city, country, "imageId", team);
Profile.ProfileBuilder profileBuilderMock = Mockito.mock(Profile.ProfileBuilder.class);
when(profileBuilderMock.build()).thenReturn(profile);
verify(profileRepository, times(1)).save(profile);
}
}
But I am getting this error:
Wanted but not invoked:
profileRepository.save(
Profile(birthDate=25.07.1996, city=Mock for City, hashCode: 997294994, country=Mock for Country, hashCode: 506775047, imageId=imageId, team=Mock for Team, hashCode: 451959555)
);
-> at com.profile.profileservice.service.ProfileServiceImplTest.addProfileTest(ProfileServiceImplTest.java:31)
Actually, there were zero interactions with this mock.
What am I missing?
Solution
First, you are not calling addProfile()
in your test. Also, you don't need to mock the ProfileBuilder
here as Profile.builder()
returns a new instance. It will not return the mocked instance.
Tip : use the given/when/then pattern for writing tests. This will help to not forget this kind of things.
@Test
void addProfileTest(){
// Given
Profile profile = new Profile("25.07.1996", city, country, "imageId", team);
// When
profileService.addProfile(profile);
// Then
verify(profileRepository, times(1)).save(profile);
}
This test passes.
Answered By - thchp
Answer Checked By - Willingham (JavaFixing Volunteer)