This chapter demonstrate about the JUnit Frame Work which is developing the test cases for the Java projects and following are the concepts covered in this chapter.
How to define a test in JUnit
JUnit naming conversions
Example JUnit test
How to define a test in JUnit
Description
In order to define a test in JUnit initially user need to develop the project and then create a test case for developed project. Following are the steps to demonstrate a test in JUnit Framework.
Step 1
In order to create the Test case initially user need to create the test project then right click on the project and
Select New - > then click JUnit Test Case
as show in below image.
Step 2
Now a window get explored named as New JUnit Test Case then enter the test case and click on the Finish button to complete the process.
JUnit naming conversions
Description
Naming conversions are defined as the naming rules of some programming languages which are applicable for the JUnit also has some set of rules to form the names of the Classes, Variables, Functions.
Class
Class name starts with the Upper case letter and it should be a noun.
Eg Splessons, SplessonTutorial, Java, Junit.
Package
Package name starts with lower case letter.
Eg splessons, splessonTutorial, java, junit.
Variable
Variable name starts with lower case letter.
Eg splessons, splessonTutorial, java, junit.
Method
Method name starts with lower case letter and it should be a verb.
Eg Main(), print(), println().
Interface
Interface name starts with upper case letter and it should be a adjective.
Eg Runnable, Remote.
Constants
Constants name starts with upper case letter.
Eg PINK, RED, MIN_PRIORITY.
Example JUnit test
Description
User need to develop a Java project, assume the project name to be as ProteinTracker, then user need to add a test method to that project some thing like ProteinTrackerTest.
The code below demonstrate the test method for the created project.
[c]
import static org.junit.Assert.*;
import org.junit.Test;
public class helloJUnitTest {
@Test
public void test() {
//fail("Not yet implemented");
}
}[/c]
Once done with the test method now its time to run the test method. The below steps demonstrate how to run the JUnit tests. Created project test method can be runned as follows,
Right click on Test -> Run As -> JUnit Test
The test case produces the output in green color which indicates as successful as shown in below image.
Summary
Key Points
JUnit Framework - Software Experts must follows the naming conversions.
JUnit Framework - While creating the Test project Add source project to the test project.
JUnit Framework - JUnit test has different test cases.