[英]I have a problem with inject Mock and Mock : Mockito with Spring Boot
Good morning,早上好,
I want to test a service: company.我想测试一个服务:公司。 However I have a problem.但是我有一个问题。 When I want to perform a test by retrieving a user from my sql file, I get an error on this user.当我想通过从我的 sql 文件中检索用户来执行测试时,我收到关于该用户的错误。 However, when I put in @Mock annotation this service, the test passes.但是,当我在此服务中添加 @Mock 注释时,测试通过了。 However, I have to put this service in @InjectMocks.但是,我必须将此服务放在@InjectMocks 中。
Could you please help me?请你帮助我好吗?
public class CompanyServiceUnitTest {
@InjectMocks
private CompanyService companyService;
@Mock
private UserCompanyService userCompanyService;
@Mock
private CompanyRepository companyRepository;
@Test
public void checkUserCompanyServiceNotCalled_WhenuserListEmpty()
throws BadRequestException, ResourceNotFoundException {
User user2 = new User();
user2.setId(3);
List<User> listUsers = new ArrayList<>();
CompanyForm companyForm = new CompanyForm();
companyForm.setName("ekeepit");
companyForm.setCity("Lille");
companyForm.setContact("Ekeep");
companyForm.setEmail("ekeepit@ekeepit.com");
companyForm.setPhone("0303034343");
companyForm.setUsers(listUsers);
Company company1 = companyService.addCompany(companyForm, user2);
verifyNoInteractions(userCompanyService);
}
and addCompany method:和 addCompany 方法:
public Company addCompany(final CompanyForm companyForm, final User organisationOwner)
throws ResourceNotFoundException, BadRequestException {
if (companyForm == null || organisationOwner == null) {
log.error("CompanyForm and OrganisationOwner can not be null");
throw new BadRequestException("CompanyForm and OrganisationOwner can not be null");
} else if (StringUtils.isEmpty(companyForm.getName()) || StringUtils.isEmpty(companyForm.getContact())
|| StringUtils.isEmpty(companyForm.getEmail()) || StringUtils.isEmpty(companyForm.getPhone())
|| StringUtils.isEmpty(companyForm.getCity())) {
log.error("All fields must be filled.");
throw new BadRequestException("All fields must be filled.");
} else if (!pattern.matcher(companyForm.getEmail()).matches()) {
log.error("Email format is not valid.");
throw new BadRequestException("Email format is not valid.");
}
Company company = Company.builder().createdAt(LocalDateTime.now())
.organization(this.organizationRepository.findOrganizationByOwner(organisationOwner)
.orElseThrow(() -> new ResourceNotFoundException(
"No organisation for the user " + organisationOwner.getId())))
.name(companyForm.getName()).disable(false).city(companyForm.getCity())
.contact(companyForm.getContact()).email(companyForm.getEmail()).phone(companyForm.getPhone()).build();
companyRepository.save(company);
if (Objects.nonNull(companyForm.getUsers())) {
List<UserCompany> userCompanies = companyForm.getUsers().stream().map(user -> {
UserCompany userCompany = new UserCompany();
userCompany.setUser(user);
userCompany.setCompany(company);
userCompany.setDisable(false);
return userCompany;
}).collect(Collectors.toList());
this.userCompanyService.add(userCompanies);
}
return company;
Your addCompany method calls organizationRepository.findOrganizationByOwner(...) so you need to mock the behaviour of this method您的 addCompany 方法调用 organizationRepository.findOrganizationByOwner(...) 因此您需要模拟此方法的行为
@Mock
private OrganizationRepository organizationRepository;
// something like this
when(organizationRepository.findOrganizationByOwner(organisationOwner)).thenReturn(Optional.of(organisation));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.