I am trying to create a Unit Test for the following method:
public List<CompanyUserDTO> findAllByUserUuidIn(final Set<UUID> userUuidList) {
return companyUserRepository.findAllByUserUuidIn(userUuidList);
}
This method returns a list of CompanyUserDTO that is interface. Here is the interface definition:
public interface CompanyUserDTO {
UUID getUserUuid();
UUID getCompanyUuid();
String getCompanyName();
default CompanyDTO getCompany() {
return new CompanyDTO(getCompanyUuid(), getCompanyName());
}
}
Here is CompanyDTO:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CompanyDTO {
private UUID uuid;
private String name;
public CompanyDTO(final Company company) {
this.uuid = company.getUuid();
this.name = company.getName();
}
}
My Unit Test is as shown below:
@Test
public void test_findAllByUserUuidIn() {
Set<UUID> userUuidList = new HashSet<>();
userUuidList.add(UUID.fromString("00000000-0000-0000-0000-000000000001"));
userUuidList.add(UUID.fromString("00000000-0000-0000-0000-000000000002"));
userUuidList.add(UUID.fromString("00000000-0000-0000-0000-000000000003"));
// --> here I need a mock list of `CompanyUserDTO`
List<CompanyUserDTO> companyUserDTOList = new ArrayList<>();
when(companyUserRepository.findAllByUserUuidIn(userUuidList))
.thenReturn(companyUserDTOList);
List<CompanyUserDTO> result = companyUserService
.findAllByUserUuidIn(userUuidList);
assertEquals(companyUserDTOList, result);
}
1. So, how should I create a mock list of CompanyUserDTO in the test?
2. Is my unit test ok with this approach?