如何使用 Mockito 测试生成器 - Java
How to test builder with Mockito - Java
我有一个用 Java 编写的构建器 class,我想用 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);
}
}
}
我有这个方法可以将配置文件添加到数据库
@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()
return 的新实例。它不会 return 模拟实例。
提示:使用 given/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);
}
此测试通过。
我有一个用 Java 编写的构建器 class,我想用 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);
}
}
}
我有这个方法可以将配置文件添加到数据库
@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()
return 的新实例。它不会 return 模拟实例。
提示:使用 given/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);
}
此测试通过。