Kuchibhotla_RamAnudeep_Review of JUnit

17
Review of JUnit Before we review JUnit, we will know the background of JUnit. A noted software engineer, Kent Beck developed Extreme Programming, a software development methodology. Besides this, he is the pioneer for developing SUnit. SUnit is a unit testing framework for Smalltalk programming language. Following this success, many XUnit series of frameworks followed paving the path for JUnit testing tool for Java programming language [1]. JUnit testing tool was developed as collective effort by Kent Beck and Erich Gamma. JUnit is one standard tool used for Test Driven Development in Java. Test Driven Development is one of the software process in which requirements act as test cases and forces the software to satisfy these tests [2]. Firstly, we will see the driving force for Unit testing i.e. motivation. Testing is a process of verifying whether the required requirements are satisfied by the system and its components. Usually, testing is done to identify gaps, errors and finding missing requirements. Unit testing includes testing of specific software components or modules by programmers or testers who possess in-depth knowledge on program design and code. The motivation for selecting Unit testing is because of the advantages mentioned below: 1. In a software project, during the development of the system, unit testing helps in finding out the bugs as soon as the developers develop units of the code. These units are checked for various scenarios. Thus making the development process in a prefixed budget and makes the application bug free. 2. Unit testing is easy to maintain and it usually results in a high quality and high productivity code. 3. Writing test cases in unit testing is easy and effortless. 4. This test involves those parts which boost the performance of those units for test which enables developers to make changes in the source code. 5. Generally, unit testing reduces the cost of change. 6. Unit testing makes development faster [. There are various unit testing techniques. There are mentioned below: 1

Transcript of Kuchibhotla_RamAnudeep_Review of JUnit

Page 1: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

Before we review JUnit, we will know the background of JUnit. A noted software engineer, Kent Beck developed Extreme Programming, a software development methodology. Besides this, he is the pioneer for developing SUnit. SUnit is a unit testing framework for Smalltalk programming language. Following this success, many XUnit series of frameworks followed paving the path for JUnit testing tool for Java programming language [1].

JUnit testing tool was developed as collective effort by Kent Beck and Erich Gamma. JUnit is one standard tool used for Test Driven Development in Java. Test Driven Development is one of the software process in which requirements act as test cases and forces the software to satisfy these tests [2].

Firstly, we will see the driving force for Unit testing i.e. motivation. Testing is a process of verifying whether the required requirements are satisfied by the system and its components. Usually, testing is done to identify gaps, errors and finding missing requirements. Unit testing includes testing of specific software components or modules by programmers or testers who possess in-depth knowledge on program design and code.

The motivation for selecting Unit testing is because of the advantages mentioned below:

1. In a software project, during the development of the system, unit testing helps in finding out the bugs as soon as the developers develop units of the code. These units are checked for various scenarios. Thus making the development process in a prefixed budget and makes the application bug free.

2. Unit testing is easy to maintain and it usually results in a high quality and high productivity code.3. Writing test cases in unit testing is easy and effortless.4. This test involves those parts which boost the performance of those units for test which enables

developers to make changes in the source code. 5. Generally, unit testing reduces the cost of change.6. Unit testing makes development faster [.

There are various unit testing techniques. There are mentioned below:

1. Black Box testing 2. White Box testing.3. Grey Box testing.

Black-Box testing is a testing method which analyses the applications functionality without considering the system internal structure, design and implementation. Black-box testing is also known as behavioral testing. Generally, tests in black-box testing are based on requirements and functionality. “Black-box testing uses the software interface and tries to ensure that the system works as expected[10]”.

White box testing is a testing method which scrutinizes the applications functionality by considering the systems internal structure, design and implementation. White box is also known as “clear-box testing or glass-box testing or structural testing [11]”. White-box testing goes beyond the user interface and into the crux of the system. Tests in white box testing rely on coverage of program statements, paths, branches and conditions.

1

Page 2: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

Grey Box testing is a testing technique usually performed by a tester with little knowledge about the functionality of the system. “These testers have access to the detailed design documents along with information about requirements. Grey Box tests are generated based on the state-based models, UML Diagrams or architecture diagrams of the target system [9]”.

Unit testing consists of various activities and steps. The steps are mentioned below:

FIG 1: FLOW OF UNIT TESTING

Step1: Create a unit test plan.

Step2: Review unit test plan.

Step 3: Code development.

Step 4: Conduct Code Walkthrough and Code Review.

2

Page 3: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

Step 5: Generate unit test cases.

Step 6: Review Unit test cases.

Step 7: Perform unit test.

Step 8: Fix bugs.

Step 9: Report unit test results [3].

Step1: Create a unit test plan

Unit test plan is made at the beginning of the project. This plan is maintained throughout the project if necessary. The various steps involved are mentioned below:

1. Usually, the unit test lead collects the inputs which basically include software development plan, SRS, high level design document.

2. The unit test lead comes up with a unit test plan.3. The unit test plan includes:

1) Tools to be used.2) Modules or units to be tested.3) Who will do the tests?[3]

Step2: Review unit test plan.

As soon as the unit test plan draft is completed, reviewing the project test plan is started. Steps involved in this are mentioned below:

1. Project manager sets an agenda for reviews.2. The project manager selects the reviewers.3. The project manager sends the unit test plan draft to the reviewers.4. The reviewers review the draft and they determine whether the unit test plan is:

1) Practical and suitable enough to be used to implement software.2) Clearly stated and reliable.

5. A review record is to be maintained for recording changes and it is to be sent to every person who gets affected with it.

6. After the review is done, unit test lead will evaluate the result.

After the step is completed, the unit test plan is approved [3]

Step 3: Code development.

In the code development step, developers develop software modules. It is performed after the requirements and several other plans are reviewed and are stable. Steps involved in code development are the following:

1. Developers start coding to develop modules and units.2. They start coding basing the SRS.

3

Page 4: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

3. The developers follow the company principles and programming strategies.4. They re-write code if there is any error and sometimes they provide feedback if necessary.

After the completion of this step, we get working modules and units [3]

Step 4: Conduct Code Walkthrough and Code Review.

In the Conduct Code Walkthrough and Code Review step, code review takes place to identify defects, errors from modules, units. Steps involved in this:

1. Developers inspect each and every line of code.2. They compile source code and check all the exceptions.3. They use various tools to detect errors, bugs.

We get an error report after the completion of this step [3].

Step 5: Generate unit test cases.

In Generate unit test cases step, the development team will perform the following:

1. They generate and document unit test cases for the units, modules.2. They perform those activities basing company coding principles [3].

Step 6: Review Unit test cases.

In Review Unit test cases step, we detect the defects present in the unit test cases. Steps involved in this are mentioned below:

1. A list of reviewers which includes unit test lead, unit testers is made by the project manager. 2. A review record should be created specifically for minor changes. After the review record is

created, it should be sent the people who get affected after the review is done.3. It is the responsibility of the unit tester to analyze and update the unit test cases after the

review is completed.

Once we are done with this step, we get the review records [3].

Step 7: Perform unit test.

In perform unit test step, the following steps are involved to execute the unit tests:

1. Create unit test executables for every unit.2. After creating the unit test executables, we must execute this executable.3. After completing with the execution, we must record and define the defects.

At the end of this step, we have the unit test reports, coded units. Perform unit test step is performed after the unit test cases are reviewed [3].

Step 8: Fix bugs.

4

Page 5: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

In fix bugs step, various steps are performed together with the unit test execution to fix bugs. The following steps are performed by the unit testers to fix the detected bugs:

1. Detect those units which are the source of the defect.2. Describe the defect and propose a solution.3. Re-write the code for the defected units.4. Test the re-written code.5. If the defect identified is correct, update the units, unit test cases or any other related

document.6. If the defect identified is incorrect, perform the test again and correct it [3].

Once the unit tester is done with correcting the defects, he forwards the units to the integrator. At the end of this step, we get the re-organized unit test reports, unit test cases and documents [3].

Step 9: Report unit test results.

After all the steps, we arrive at the report unit test results. In this step, the following steps are involved:

1. The unit test lead collects all the information i.e. unit defects list, unit test reports from the unit tester.

2. After the unit test lead collects all the required information he creates unit test reports.3. After creating unit test report, he sends it to the project manager.

After completing this step, we finally have the unit test reports [3].

JUnit testing procedure:

FIG 2: JUnit testing procedure

The JUnit testing process from writing a relevant test class to obtaining the final test results is quite simple. Below we can see how this process might work.

5

Page 6: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

Here, we will look into the JUnit testing procedure i.e. stating with writing a test class and tracking down the test results. For this instance, we will choose a class called “class X” and we will test class x test units. In order to test, we are supposed to have a test class. So we create test class called as “ TestClassX”. This TestClass X will then import the relevent classes form the JUnit library. The JUnit framework comes with a JUnit tool runner which runs the programs and checks for errors. JUnit tool will check each each unit and verify whether the unit failed or passed the test. After the checking is done, it will produce the output and it shows the exact line of error or bug. The developers will check the errors and will eventually makes changes to the code of class X and will recheck it by executing the program using JUnit tool runner and make sure that the units function as expeceted.

JUnit framework include the following terminologies:

1. Test Fixtures.2. Test Class.3. Test Suite.4. Test Runners5. Test pass.6. Test Fail.7. Assertions.8. checks if the code is clean. If it is clean, the bar turns green [6].

Test Fixtures provides support for software systems to perform their respective operations. Test Fixture contains two methods.

1. setUp() method runs before a test starts.2. teardown() methods runs after a test starts.

Test Class is java program which contains unit tests. Suppose for class X, the tests are written in TestClass X.

TestSuite is combination of various unit tests.

Test Runner is JUnit tool for executing the tests.

Assertions : The purpose of a test is to check whethet the desired output is achieved or not. This is done by changing the values of the objects and checking their outcome. Every test method uses these assertions to express the desired outcome and are checked whther it has been achieved.

Test pass: when a test method is sucessfully executed, then the test passes.

Test fail: When a test method is executed, if any of its assertions fail, then the test fails.

There are some annotations which are introduced from JUnit 4. They are mentioned below:

1. @Test Public void method()2. @Before public void method()3. @After public void method()

6

Page 7: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

4. @Before class public void method()5. @After class public void method()6. @ignore7. @(Test expected= exception class)8. @Test(timeout =100)

@Test Public void method(): The annotation identifies a method as a test method.

@Before public void method(): The annotation executes the method prior testing. This will arrange the test environment.

@After public void method(): The annotation will execute after every test. This will clean the used memory.

@Before class public void method(): This method is executed before we start a test. “This method is used to perform time intensive activities”.

@After class public void method(): This method is execcuted after all tests are done. Genrally performed for cleaning up the activites.

@ignore: Sometimes we ignore some of the test cases due to change in code or execution time is too long.

@(Test expected= exception class): This usually fails if the method doesnot throw the expected exception.

@Test(timeout =100): This usually fails if the methods execution longer than 100 milliseconds [6].

There are some test methods which are mentioned below:

1. Void assertEquals(Boolean expectd, Boolean actual) : This method checks whether the objects are equal.

2. Void assertTrue(Boolean expected, Boolean actual): This method checks if the condition is true.

3. Void assertFalse(Boolean condition): This method checks if the condition is false.4. Void assertNotNull (object object): This method checks whether an object is not null.5. Void assertNull( Object object): This method checks whether an object is null.6. Void assertSame(Boolean condition): This method checks if two objects refer to the

same object.7. Void assertNotSame(Boolean condition): The assertNotSame() method tests if two

objects do not refer to the same object.8. Void assertArrayEquals(expectedArray,resultArray):“This method tests whether two

arrays are equal to each other” [6].

We will see the procedure of creating a JUnit project in Eclipse IDE.

7

Page 8: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

1. Firstly, we should create a Java project.2. JUnit.jar is available in Eclipse IDE and we must add libraries.3. In eclipse, we have to add library to the build path.4. After adding the libraries, we follow the below mentioed order to select a test.

Project - > New -> source folder - > test.5. We then click on test and select JUnit test case [4].

Now, we will demonstrate JUnit testing on a program in eclipse IDE.

We first create a project with a name Tested1 then we build a sample class Temp. This Temp class is the main class. In Temp class, we have methods for addition, subtraction, pre increment and pre decrement. These methods are public. In this we have two paramaetrs for addtion and subtraction and one parameter for pre increment and pre decrement. In Testcase class, we start by importing assert package from the JUnit library.

SAMPLE CLASS TO BE TESTED:

public class Temp {

public int add(int a,int b){

return(a+b);

}

public int sub(int a,int b){

return(a-b);

}

public int inc(int a){

return(++a);

}public int dec(int a){

return(--a);}}

TEST CASE:

import static org.JUnit.Assert.*;

import org.JUnit.Test;

public class Testcase {

@Test

public void test() {

8

Page 9: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

Temp t=new Temp();

int a=t.add(11,20);

assertEquals(30,a);

int b=t.sub(10,20);

assertEquals(-10,b);

int c=t.inc(10);

assertEquals(10,c);

int d=t.dec(10);

assertEquals(9,d);

}

}

In the above program, the Temp class performs some arithmatic operations. the correctness of the values returened by the Temp class are checked using the Testcase class. The correctness is checked by the assertEquals method imported from static org.JUnit.Assert.*;

For instance, addition method accepts two parameters a and b. this method returns the sum of two numbers and is checked using the assertEquals method in the Testcase class. If the value returned is equal to the value specified then the test case passes the test other wise it fails the test.

Similarly, for the other methods subtraction, pre-increment and pre-decrement we pass the values basing the parameters. After the input is given, the values are checked for equality using the assertsEqual method. If the test case passes it is shown in a green box in a new window. If the test case fails it is shown in a red box.

The following are the screenshots of the program:

9

Page 10: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

FIG 3: creation of Java project.

FIG 4: Temp class.

10

Page 11: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

FIG 5: Testcase class

FIG 6: Testcase pass result

11

Page 12: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

FIG 7: Testcase fail result

Advantages of JUnit:

1. JUnit helps developers in writing test cases while developing the software. This allows them to test their code line by line enabling them to write an efficient and bug free code. The software they built is tested before the module is tested.

2. JUnit permits the developers to make modification to the written code without affecting the other units which are performing as expected.

3. A new member can easily understand the test cases written using JUnit and can efficiently manage them. Thus ultimately giving his best to develop robust software.

4. JUnit is compatible with many IDE’s. IDE’s which support JUnit allows a user to test Java programs using JUnit wizards available which improves output. Examples of IDE’s are NetBeans and Eclipse.

5. JUnit integrates with tools like ANT and maven allowing to capture results of the test cases and document them [7].

6. “JUnit framework has methods like setUp and tearDown for standard resource initialization”.

Disadvantages of JUnit:

1. JUnit is not appropriate for higher level testing i.e. not suitable for bigger programs.2. It does not support dependency testing.

Summary:

12

Page 13: Kuchibhotla_RamAnudeep_Review of JUnit

Review of JUnit

Generally, No code is perfect in the initial stages i.e. during the development stages. There might be instances where a code does not perform as expected in the later stages of development. In order to overcome this we make changes to the code. Mostly, Bugs in the program might be due to poor requirement analysis which will eventually increase the cost and effort to be spent on correcting the errors or faults. My understanding is that Unit testing helps in detecting bugs very early in the project i.e. before developers develop units of code. Using the divide and conquer method, unit testing divides the code into units and check for errors or bugs ultimately making testing easy and simple. Testing is done using testing tools. For java programming, we use JUnit testing framework. The sole purpose of using JUnit is it limits the number of bugs in the final deliverable. More importantly, the combination of JUnit and Test Driven Development result in effective code which eventually reduces the effort and cost spent in testing.

References:

[1] Wikipedia, https://en.wikipedia.org/wiki/JUnit.[2] Wikipedia, https://en.wikipedia.org/wiki/Test-driven_development[3] Khannur, http://www.khannur.com/sst7.9-continued.htm[4] Dzone, https://dzone.com/articles/JUnit-tutorial-beginners [5] Tutorialspoint,http://www.tutorialspoint.com/software_testing_dictionary/

unit_testing.htm[6] Slideshare,http://www.slideshare.net/pallavikhandekar212/JUnit-40[7] Gontu,http://www.gontu.org/benefits-of-using-JUnit-framework/ [8] Softwaretestinghelp,http://www.softwaretestinghelp.com/unit-testing/[9] Tutorialspoint,http://www.tutorialspoint.com/software_testing_dictionary/

grey_box_testing.htm[10] Black-box testing, Wikipedia, https://en.wikipedia.org/wiki/Black-box_testing [11] Software testing help,http://www.softwaretestinghelp.com/white-box-testing-

techniques-with-example/ [12] Software testing help, http://www.softwaretestinghelp.com/types-of-software-

testing/

13