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.

  • 5
    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, 2013 at 14:46
  • @DavidO: "Is that object populated from the DB as expected." is decidedly not a unit test (in regards to OP's code). That's an integration test.
    – Flater
    Oct 31, 2019 at 8:05
  • Yes, you are correct. If I could go back and change the comment to mention that in an integration test you would verify the DB component, and otherwise mock it, I would make that edit, but the edit window for comments if 5 minutes, and the comment was made six years ago. :)
    – DavidO
    Oct 31, 2019 at 22:31

1 Answer 1


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;
    // 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.

  • 4
    Usually you don't have logic within your DTO classes, especially none that interact with your data storage. Mar 26, 2013 at 15:42
  • 1
    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, 2013 at 15:49

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