Home > Software design >  How to mock an interface in Unit Testing in Java?
How to mock an interface in Unit Testing in Java?

Time:11-19

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?

CodePudding user response:

  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);
}
  • Related