Issue
My first method that I'm trying to validate, it receives an entity and gets the date to do the validation:
public boolean validaDataItem(RequestPedidoDTO requestPedidoDTO) {
boolean valido = true;
List<Item> itemDTOS = requestPedidoDTO.getItens();
for (int i = 0; i < requestPedidoDTO.getItens().size(); i++) {
valido = itemDTOS.get(i).getData_validade()
.isBefore(itemDTOS.get(i).getData_criacao());
if(itemDTOS.get(i).getData_criacao()
.isAfter(itemDTOS.get(i).getData_validade())){
throw new DataInvalidaException();
}
}
return valido;
}
My second method that I'm trying to validate, it receives an entity and enters a list inside that entity to do the validation:
public boolean validaDataOfertas(RequestPedidoDTO requestPedidoDTO) {
boolean valido = true;
List<Item> itemDTOS = requestPedidoDTO.getItens();
for (int i = 0; i < requestPedidoDTO.getItens().size(); i++) {
for(int j = 0; j < requestPedidoDTO.getItens().get(i).getOfertas().size(); j++) {
valido = itemDTOS.get(i).getOfertas().get(j).getData_validade()
.isBefore(itemDTOS.get(i).getOfertas().get(j).getData_criacao());
if (itemDTOS.get(i).getOfertas().get(j).getData_criacao()
.isAfter(itemDTOS.get(i).getOfertas().get(j).getData_validade())) {
throw new DataInvalidaException();
}
}
}
return valido;
}
My entity requestPedidoDTO:
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class RequestPedidoDTO {
@NotBlank
@CPF(message = "CPF inválido! Deve seguir o padrão de 11 digitos")
private String cpf;
@NotNull @Positive
private Double total;
private List<@Valid Item> itens;
}
My entity Item:
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Item {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String nome;
@NotNull
@JsonFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private LocalDateTime data_criacao;
@NotNull
@JsonFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private LocalDateTime data_validade;
@NotNull @Positive
private Double valor;
@NotBlank
private String descricao;
@ManyToMany(cascade = CascadeType.ALL)
private List<Oferta> ofertas;
}
My entity Oferta:
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Oferta {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String nome;
@NotNull
@JsonFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private LocalDateTime data_criacao;
@NotNull
@JsonFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private LocalDateTime data_validade;
@NotNull @Positive
private Double desconto;
@NotBlank
private String descricao;
}
Additions I made to the test to make it work:
Solution
Your functions do not need any mocks because they do not have any dependency on other classes, what you can do to unit test your functions is purely create an object of your entity and call the function with that entity, this will allow you to test your functions to be unit tested. Some examples of the unit tests you can write
- Check for
true
condition
@Test
public void test_validaDataItem_true {
RequestPedidoDTO requestPedidoDTO = new RequestPedidoDTO();
//Initialize the object with a DTO that should return true
boolean valid = validaDataItem(requestPedidoDTO);
Assert.assertTrue(valid);
}
- Check for
false
condition
@Test
public void test_validaDataItem_true {
RequestPedidoDTO requestPedidoDTO = new RequestPedidoDTO();
//Initialize the object with a DTO that should return false
boolean valid = validaDataItem(requestPedidoDTO);
Assert. assertFalse(valid);
}
- Check for
DataInvalidaException
@Test(expected=DataInvalidaException){
RequestPedidoDTO requestPedidoDTO = new RequestPedidoDTO();
//Initialize the object with a DTO that should throw a Data Invalid Exception
validaDataItem(requestPedidoDTO);
}
Answered By - Sarah N
Answer Checked By - Cary Denson (JavaFixing Admin)