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. However, when I put in @Mock annotation this service, the test passes. However, I have to put this service in @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("[email protected]");
companyForm.setPhone("0303034343");
companyForm.setUsers(listUsers);
Company company1 = companyService.addCompany(companyForm, user2);
verifyNoInteractions(userCompanyService);
}
and addCompany method:
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;
CodePudding user response:
Your addCompany method calls organizationRepository.findOrganizationByOwner(...) so you need to mock the behaviour of this method
@Mock
private OrganizationRepository organizationRepository;
// something like this
when(organizationRepository.findOrganizationByOwner(organisationOwner)).thenReturn(Optional.of(organisation));