Unit testing in Java
1) Describe the notation of exception handling
2) React correctly when certain exceptions occur
3) Use Java’s exception-handling facilities effectively in classes and programs
An exception is an object that signals the occurrence of an unusual event during the execution of a program. The process of creating this object that is generated an exception is called throwing an exception.
Unit testing can be done in two ways − manual testing and automated testing.
1. Executing a test cases manually without any tool support is known as manual testing.
2. Time-consuming and tedious − Since test cases are executed by human resources, it is very slow and tedious.
3. Huge investment in human resources − As test cases need to be executed manually, more testers are required in manual testing.
4. Less reliable − Manual testing is less reliable, as it has to account for human errors.
5. Non-programmable − No programming can be done to write sophisticated tests to fetch hidden information.
6. Taking tool support and executing the test cases by using an automation tool is known as automation testing.
7. Fast − Automation runs test cases significantly faster than human resources.
8. Less investment in human resources − Test cases are executed using automation tools, so less number of testers are required in automation testing.
9. More reliable − Automation tests are precise and reliable.
10. Programmable − Testers can program sophisticated tests to bring out hidden information.
A Unit Test Case is a part of code, which ensures that another part of code (method) works as expected. To achieve the desired results quickly, a test framework is required. JUnit is a perfect unit test framework for Java programming language.
A formal written unit test case is characterized by a known input and an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a post condition.
There must be at least two unit test cases for each requirement − one positive test and one negative test. If a requirement has sub-requirements, each sub-requirement must have at least two test cases as positive and negative.
Write a java program that catches arithmetic exception.
A java program for an array declared with 2 elements. Then the code tries to access the 3rd element of the array which throws an exception.
Java program to access the index in an array that is not present in it.
Java program for multiple try catch blocks.
Java program for nested try loops
Create your own exception sub class simply by extending java Exception class. You can define a constructor for your Exception sub class (not compulsory) and you can override the toString() function to display your customized message on catch.
Write a statement that will throw an exception of type Exception if the value of the String variable named status is “bad”. The string recovered by getMessage should be “Exception thrown: Bad Status.” You need not write the try block and catch block.