-2

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

Norcal
  • 13
  • 5
  • Two answers from previous days regarding Unit Testing: http://stackoverflow.com/questions/38081611/junit-writing-a-test-for-a-method-that-deletes-an-entity/38082803#38082803 and http://stackoverflow.com/questions/37947075/how-to-testmock-object-that-uses-external-api-jama-software/37948445#37948445 – Compass Jul 07 '16 at 20:57

2 Answers2

0

There are a few rules to Unit Testing:

  1. Test None - test when the input is none or zero
  2. Test One - test when the input is one, or length one, etc.
  3. Test Many - test when the input is greater than one, or longer than one
  4. Test first - test what happens when your input is the first thing
  5. Test last - test what happens when your input is the last thing
  6. Test all paths of code - As you have all these if statements, test all the combinations of the if statements.

A good way to see how effective your testing is, is to use a code coverage tool and see how many times, and which lines are being hit in testing. For java try using JaCoCo.

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
0

A better way to start doing unit testing is to decide what you want your code to do and then write tests to make sure it does those things, AND ONLY THEN write your code. Ideal unit tests will have a test case that covers each line of code, every if statement and edge case, and the answer contributed by Eli Sadoff pretty much covers those cases.

  • Thank you so much, I just came into this code already written. In school that was the best practice to write tests first and code second but this was not the case. – Norcal Jul 07 '16 at 21:21