jeudi 7 juillet 2016

Advice on Unit Tests

I am wanting to create some unit tests on a couple blocks of code but am unsure of what I should be testing, Unit testing is new to me and I learn better by example. Here are the code in question.

public static String buildAddressStreet(Address address)
{
  if(address.getAddressLines().size() > 0)
  {
     return address.getAddressLines().get(0);
  }
  else
  {
     return StringUtils.EMPTY;
  }
}


 public static Collection<Payment> collectFraudPayments(WebOrder order)
{
  return streamPaymentsFilterForFraud(order).collect(Collectors.toList());
}

private static Stream<Payment> streamPaymentsFilterForFraud(WebOrder order)
{
  return order.getPayments().stream()
     .filter(i -> i.getPayMethod().isCreditCard())
     .filter(i -> (!StringUtils.startsWith(i.getFraudStatusCode(), "A") ||  FraudStatusCode.isPossibleFraud(i.getDecisionResponse())) );
}


public static String getAddressLocation(Address address, int location)
{
  if(location < 3)
  {
     if(address.getAddressLines().size() >= location+1)
     {
        return address.getAddressLines().get(location);
     }
  }

  return null;
}


public static String getCommerceCustomerNumber(WebOrder order)
{
     Customer customer = findCustomer(order);

  if(customer != null)
  {
     return customer.getId();
  }
  else
  {
     return null;
  }
}


public static String buildCustomerName(WebOrder order)
{
  Optional<? extends Customer> foundCustomer =   order.getItems().stream().findFirst().map(i -> i.getShipping());

  if(!foundCustomer.isPresent())
  {
     foundCustomer = order.getPayments().stream().findFirst();
  }

  return buildCustomerName(foundCustomer.orElse(null));
}
public static String buildCustomerName(Customer customer)
 {
  StringBuilder sb = new StringBuilder();

  if(customer != null)
  {
     if(StringUtils.isNotBlank(customer.getAddress().getFirstName()))
     {
        sb.append(customer.getAddress().getFirstName()).append(' ');
     }
     if(StringUtils.isNotBlank(customer.getAddress().getMiddleName()))
     {
        sb.append(customer.getAddress().getMiddleName()).append(' ');
     }
     if(StringUtils.isNotBlank(customer.getAddress().getLastName()))
     {
        sb.append(customer.getAddress().getLastName()).append(' ');
     }
     if(StringUtils.isNotBlank(customer.getAddress().getCompanyName()))
     {
        sb.append(customer.getAddress().getCompanyName()).append(' ');
     }

  }

  return sb.toString().trim();
}

I understand this is prob a lot, I am not wanting the code written for me just an idea or example of how this would work to test this. I have written basic tests before but some of these have me a bit stumped. Thanks

Aucun commentaire:

Enregistrer un commentaire