Unit testing in Java
Activity Outcomes:
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
Introduction
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.
Manual 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.
Automated Testing
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.
Lab Activities:
Activity 1:
Write a java program that catches arithmetic exception.
Solution:
Activity 2:
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.
Solution:
Activity 3:
Java program to access the index in an array that is not present in it.
Solution:
Activity 4:
Java program for multiple try catch blocks.
Solution:
Activity 5:
Java program for nested try loops
Solution:
Home Activities:
Activity 1:
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.
Activity 2:
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.