JUnit Interview Questions and Answers
Ques 21. How Often Should You Run Your JUnit Tests?
Ans. You should run all your unit tests as often as possible, ideally every time the code is changed. Make sure all your unit tests always run at 100%. Frequent testing gives you confidence that your changes didn\'t break anything and generally lowers the stress of programming in the dark.
For larger systems, you may just run specific test suites that are relevant to the code you\'re working on. Run all your acceptance, integration, stress, and unit tests at least once per day.
Ques 22. Do You Have To Write a Test for Everything?Ans. No, just test everything that could reasonably break. Keep in mind NullPointerException which occurs maximum.
Be practical and maximize your testing investment. Remember that investments in testing are equal investments in design. If defects aren\'t being reported and your design responds well to change, then you\'re probably testing enough. If you\'re spending a lot of time fixing defects and your design is difficult to grow, you should write more tests.
If something is difficult to test, it\'s usually an opportunity for a design improvement. Look to improve the design so that it\'s easier to test, and by doing so a better design will usually emerge.
Ques 23. How Do You Test a Method That Does not Return Anything?Ans. You need to follow the below logics:
* If a method is not returning anything through the \"return\" statement (void method), it may return data through its arguments. In this case, you can test the data returned in any argument.
* Else if a method is not returning any data through its arguments, it may change values of its instance variables. In this case, you can test changes of any instance variables.
* Else if a method is not changing any instance variable, it may change values of its class variables. In this case, you can test changes of any class variables.
* Else if a method is not changing any class variable, it may change external resources. In this case, you can test changes of any external resources.
* Else if a method is not changing any external resources, it may just doing nothing but holding the thread in a waiting status. In this case, you can test this waiting condition.
* Else if a method is not holding the thread in waiting status, then this method is really doing nothing. In this case, there is no need to test this method!
Ques 24. When Should Unit Tests Should Be Written In Development Cycle?Ans. You should write unit test before writing the code if you are a TDD (Test-Driven Development) believer. Test-first programming is practiced by only writing new code when an automated test is failing.
Good tests tell you how to best design the system for its intended use. They effectively communicate in an executable format how to use the software. They also prevent tendencies to over-build the system based on speculation. When all the tests pass, you know you\'re done!
Whenever a customer test fails or a bug is reported, first write the necessary unit test(s) to expose the bug(s), then fix them. This makes it almost impossible for that particular bug to resurface later. Test-driven development is gaining momentum these days compared to writing tests after the code.
Most helpful rated by users: