0

I am trying to undarstand if I am writing unit test in the write way. I have a hashmap where I store my customer register. I am trying to write a unit test for my createCustomer metod. Can someone give me pointers if I am in the right direction?

void addCustomer () {
        System.out.println ();

        String customerName = getString ("Enter Customer Name with cappital letar: ");

        String customerAddress = getString ("Enter Customer Address with cappital letar: ");

        int customerPhone = getInt ("Enter Customer phone:");

        int customerID = checkID ();
        Customer customer = new Customer (customerName, customerAddress, customerID, customerPhone);
        customerList.put (customerID, customer);
        System.out.println ("Customer Added");

    }

@Test
    public void addCustomerTest () {
        HashMap<Integer,Customer> customerList = new HashMap<> ();
        String customerName = "Anna";
        String customerAddress = "London";
        int customerPhone =  1010101;

        int customerID = 1000;
        Customer customer = new Customer (customerName, customerAddress, customerID, customerPhone);
        customerList.put (customerID, customer);

        assertTrue(customerList.containsKey(customerID) && customerList.get(customerID) != null);

    }
Kras
  • 3
  • 2
  • 1
    This is pretty much off-topic for SO, but your test is exposing a design problem in your code. You are mixing UI, where you get multiple values from a user and then create an object with this state before saving it to a global somewhere. A unit test would just test the creation of a Customer object, where you assert that null or invalid arguments are correctly allowed for. –  Feb 05 '19 at 15:33

2 Answers2

1

You are not the HashMap author while currently you unit test this class.
So no you don't test in the right way your code.
What you want unit testing is the API of your class : that is addCustomer().
The Map is an implementation detail that may change over the time and that you don't want to test.

Your unit test should look like :

@Test
public void addCustomer() {
    CustomerRepository repo = new CustomerRepository();
    String customerName = "Anna";
    String customerAddress = "London";
    int customerPhone =  1010101;
    int customerID = 1000;
    // Mock the System IN to read these values
    // ...
    // invoke the method under test
    repo.addCustomer();
    // assert that the repo contains the new object
    Customer actual = repo.findCustomerById(customerID);
    assertNotNull(actual);
    assertEquals(customerName, actual.getCustomerName());
    assertEquals(customerID, actual.getCustomerID());
    // and so for for each field
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

When you write a unit test you are testing a unit of code that you wrote.

In the test

@Test
public void addCustomerTest () {
    HashMap<Integer,Customer> customerList = new HashMap<> ();
    String customerName = "Anna";
    String customerAddress = "London";
    int customerPhone =  1010101;

    int customerID = 1000;
    Customer customer = new Customer (customerName, customerAddress, customerID, customerPhone);
    customerList.put (customerID, customer);

    assertTrue(customerList.containsKey(customerID) && customerList.get(customerID) != null);

}

you are not testing your code, but eventually you are testing the HashMap.

To write a good unit test you need to:

  • identify the method that you wrote and that you like to test.
  • if your method accept parameters identify the border values that can create problems to your code (for example a null value for an object, an empty list for a list, a maximum or minimum integer for an int)
  • write a test to check if your code works for those special values
  • write one test to check if it works with normal values

If all works you can try to rewrite the code refactoring it to have a better design, following the mantra

  • RED - think about what you want to develop
  • GREEN - think about how to make your tests pass
  • Refactor - think about how to improve your existing implementation

then add new tests and follow again the process red, green, refactor.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56