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:You can write your own mocked and dirty
CustomerRepository
class the following way: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 ofgetCustomer
: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.