Google News
logo
JUnit - Interview Questions
What are some best practices you follow while writing code to make it more testable?
Here are some of the best practices you can follow to make your code more testable :
 
* Use interface instead of concrete class, this allows the tester to replace actual class with Mock or Stub for testing purposes.
 
* Use Dependency injection, it makes it easy to test individual parts and supply dependency from test configuration. You can create an actual object which is dependent on the Mock and stub for testing. This also allows you to test a class even if its dependency is not yet coded.
 
* Avoid static methods, they are difficult to test as they cannot be called polymorhpically.
Advertisement