3

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?

0

1 Answer 1

4
  1. You don't need a mock, just create the objects themselves (I guess that you have at least an implementation of the interface CompanyUserDTO).
  2. Everything else is ok, although your method does not have that much logic to be tested.

Given that you don't have an implementation for CompanyUserDTO you might make usage of anonymous classes to avoid having an implementation per se but something just for testing purposes. You could do it as follows:

@Test public void test_findAllByUserUuidIn() { // Arrange 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")); CompanyUserDTO companyUserDTO = new CompanyUserDTO() { @Override public UUID getUserUuid() { return UUID.randomUUID(); } @Override public UUID getCompanyUuid() { return UUID.randomUUID(); } @Override public String getCompanyName() { return "Company Name Test"; } }; List<CompanyUserDTO> companyUserDTOList = new ArrayList<>(); companyUserDTOList.add(companyUserDTO); when(companyUserRepository.findAllByUserUuidIn(userUuidList)) .thenReturn(companyUserDTOList); // Act List<CompanyUserDTO> result = companyUserService .findAllByUserUuidIn(userUuidList); // Assert assertEquals(companyUserDTOList, result); } 

If you prefer to avoid anonymous classes, then you need to create the following class in your test source package:

public class CompanyUserDTOImpl implements CompanyUserDTO { @Override public UUID getUserUuid() { return UUID.randomUUID(); } @Override public UUID getCompanyUuid() { return UUID.randomUUID(); } @Override public String getCompanyName() { return "Company Name Test"; } }; 

And then your test would be something simpler:

@Test public void test_findAllByUserUuidIn() { // Arrange 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")); List<CompanyUserDTO> companyUserDTOList = new ArrayList<>(); companyUserDTOList.add(new CompanyUserDTOImpl()); when(companyUserRepository.findAllByUserUuidIn(userUuidList)) .thenReturn(companyUserDTOList); // Act List<CompanyUserDTO> result = companyUserService .findAllByUserUuidIn(userUuidList); // Assert assertEquals(companyUserDTOList, result); } 
Sign up to request clarification or add additional context in comments.

27 Comments

Do you have any concrete implementation for the interface CompanyUserDTO?
Not concrete implementation, just used for some mapping e.g. .map(CompanyUserDTO::getCompanyUuid)
Ok, but for that mapping you need to have an object that implements CompanyUserDTO, otherwise it will not work. So my question remains do you have any class implementing CompanyUserDTO?
I searched on the project by phrase of "implements CompanyUserDTO", but there is not any class or file.
Are you using any mapping library such as MapStruct for example?
|