Unit-testing – How to unit-test the REST web service

integration-testsresttestingunit testing

I am new to unit testing, I've one REST web method that just calls DB and populates a DTO. Pseudo code is

public object GetCustomer(int id)
{
  CustomerDTO objCust = //get from DB
  return objCust;
}

My doubt is how to write tests for these methods and type of tests (Integration/Unit) to be included. And for unit tests, does it need to hit the DB. If it were and I pass a customer id and do few assertions, the data might change eventually resulting in failures.

I think I am missing something here understanding these concepts.

Best Answer

While unit testing you are not expected to test with a database, or at least, not with a database you haven't prepared for unit testing. Testing with a database, and as such, testing different layers of your application at the same time is generally seen as integration tests. With unit tests you are supposed to test only what your method does, what it returns depending on different parameters, and when (or not) it should fail.

It is very much expected that in your method you make calls to X methods from other classes. You are not testing these X methods so what you have to do is to mock these methods.

I suppose you're writing your code in Java, in that case you have great mocking frameworks such as Mockito which may be helpful to you. Whether or not you use a mocking framework is your choice, I'll just say they'll save you a lot of time and the one I mentioned at least is really not complicated.

If you just want to write your own mock to experiment, then suppose you have the following CustomerRepository class:

public class CustomerRepository {
 public CustomerDTO getCustomer(int id) {
   ...
 }
}

You can write your own mocked and dirty CustomerRepository class the following way:

public class MockedCustomerRepository extends CustomerRepository {
 public boolean bThrowDatabaseException;
 public boolean bReturnNull;
 public boolean bReturnCustomerWrongId;
 public boolean bReturnCustomerWithId;
 public CustomerDTO getCustomer(int id) {
  if(bThrowDatabaseException) { 
    throw new DatabaseException("xxx"); 
  } else if(bReturnNull) { 
    return null; 
  } else if(bReturnCustomerWrongId) { 
    throw new CustomerNotExistException(id);
  } else if(bReturnCustomerWithId) { 
    return new CustomerDTO(id); 
  }
 }
}

Then, in your test case you basically replace your "standard" instance of CustomerRepository with a mocked instance that will allow you to test your method for various outcomes of getCustomer:

public class CustomerRestTest {
  public void testGetCustomer_databaseFailure() {
    MockedCustomerRepository dto = new MockedCustomerRepository();
    dto.bThrowDataBaseException = true;
    yRestClass rest = new MyRestClass();
    rest.dto = dto;
    rest.getCustomer(0);
    // depending on what you do in your getCustomer method, you should check if you catched the exception, or let it pass, etc.. Make your assertions here

  public void testGetCustomer_customerNotExist() {
    // etc.
  }
}

Generally, every test method should test one thing only, this helps keep your tests small and focused on one task.

I'm going to repeat it :-) Writing a whole mocked class takes some time as you see. Consider using a mocking framework, the less one writes code, the less errors one makes, right? Mocking a method that throws an exception, or returns a given value for a given parameter is a piece of cake and takes 2 or 3 lines (with mockito at least)

Hope that helps testing your REST method.