Performing regression testing for each unit in the application is done by using JUnit testing framework in java. To know whether our created component of program are performing as expected or not, we can use JUnit testing framework.
For the sake of simplicity let's perform a JUnit testing (in my case using Netbeans IDE)
for method which simply adds two integer and return the sum of two integers.
Using our Netbeans IDE we can create test files for each individual file, by clicking class name inside the package, hover to tools then select create/update tests.
For the sake of simplicity let's perform a JUnit testing (in my case using Netbeans IDE)
for method which simply adds two integer and return the sum of two integers.
Class with definiton of sum method |
After clicking create Update/Test options we are instructed to select the Testing framework, leave it as default and click ok.
After selecting appropriate version of JUnit framework file with classname appended with 'Test' is created inside Test Packages which includes the method inside our class with test before the actual method name and uppercase for first letter of method name.
Above program test the sum method inside the 'Calculator ' class. Since the argument of sum method are of integer type, testSum() method is testing for scenario when user provide maximum integer value to add in both parameters. assertEquals() method check for whether expected result (expResult) and actual result obtained (result) is same or not. assertEquals() method calls equals method on each object to check equality. If compared objects are not equal test an assertion error without a message is thrown. Above test file can be executed by simply running the test file which gives test result as:
Hence by using JUnit testing framework we have successfully tested for sum method inside calculator class, likewise we can test for each method inside calculator class.
Comments
Post a Comment