如何使用Mockito - Java测试构建器



我有一个用Java编写的构建器类,我想用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);
}
}
}

我有这个方法来添加Profile到数据库

@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);
}

我试着像这样测试它:

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);
}
}

但是我得到这个错误:

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.

我错过了什么?

首先,您没有在测试中调用addProfile()。此外,您不需要在这里模拟ProfileBuilder,因为Profile.builder()返回一个新实例。它不会返回模拟的实例。

提示:使用给出的/when/then模式来编写测试。这将有助于不要忘记这类事情。
@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);
}

测试通过。

最新更新