I am wriring unit test cases for an existing system. The architecture for the underlying classes if very complex in itself.
Blockquote
RequestHanndler ==> processes ==> Order ===> is dependent on ==> service layer == connected to ==> DB layer.
I am writing a test case for RequestHandler. The method in test(doProcess()) creates a new instance of Order class. Order class itself has very tight dependency on the service layer. I want to create an atomic test case, so, not any other layer of code will be executed.
What should the best process to create test cases for these scenrios?
It might get a bit complicated when you want to write unit-tests for tighly coupled code. To make uni-testing easier you should better rely on abstractions and not on real implementations. E.g. the
Orderclass shouldn’t depend on the real implementation of the service layer, instead introduce an interface which is much easier to mock instead of a class which might be set tofinal.Since your RequestHandler is responsible for creating the Order instances you’ll have to provide a way to mock out the order class in unit-tests. A simply way is to create a protected method that simply creates a new order instance.
In your Unit-Tests you can now extend the class and overwrite the factory-method.
Using Mockito this would look like: