Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
4  
In the code you posted, the things to test are: (1) Can you call GetCustomer with an int as a parameter. (2) Does it return a CustomerDTO object? (3) Is that object populated from the DB as expected. (4) Does an expected behavior occur if called with an int that doesn't correspond to a valid customer? None of that has to do with REST yet. When you are ready to write the code that responds to RESTful requests, then you'll write tests for it. –  DavidO Mar 26 '13 at 14:46
    
Thank you for your inputs. –  Sunny Mar 26 '13 at 23:06

1 Answer 1

up vote 10 down vote accepted

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.

share|improve this answer
3  
Usually you don't have logic within your DTO classes, especially none that interact with your data storage. –  JustAnotherUserYouMayKnowOrNot Mar 26 '13 at 15:42
    
It was just an example, but you're absolutely right. I'm going to change the examples so that they better suit the theory. –  Jalayn Mar 26 '13 at 15:49
    
Thanks a lot! It helped. –  Sunny Mar 26 '13 at 23:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.