Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
JUnit
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Writing and testing method of addition of two numbers

                         


This section starts with a simple example that will illustrate the basic concepts involved in testing with JUnit. In this section, we will be creating a java file named Calculator.java which has a method named sum() which takes two int parameters and return addition of these two numbers. So here we have to check whether this method is functioning well in all the conditions or not. 

Creating Calculator.java :
This class has sum method which takes two int parameters to add them and return it . Save and compile this file.

public class Calculator{
    int sum(int num1,int num2){
        return num1+num2;
    }
}

Creating CalculatorTest.java :
To test that method sum() is working fine we need to check it. For this we create another class named CalculatorTest. Save and compile this file. Before proceeding further we should first have a look over JUnit coding convention.
        Coding Convention :
   
             1. Name of the test class must end with "Test".
                2. Name of the method must begin with "test".
                3. Return type of a test method must be void.
                4. Test method must not throw any exception.
                5. Test method must not have any parameter.
In our example, the class name which we are going to test is "Calculator" so we have created class "CalculatorTest" here. In the same way the method created to test the particular method will be appended with test word as in this example we are going to test the method sum() hence we have created method testSum() in this class. 

import junit.framework.TestCase;

public class CalculatorTest extends TestCase {
    Calculator cal=new Calculator();
    
     public CalculatorTest(String name) {
             super(name);
     }
     
     public void testSum() {
             assertEquals(2,cal.sum(1,1));
    }
}

Explanation :

import junit.framework.TestCase;

As we need to use some classes of JUnit constructs in the testing program so have to use import statement to use them. In this example, we are going to use TestCase class so we need to import this class from framework package of JUnit.

public class CalculatorTest extends TestCase

If we want to define our own test methods then we have to extend TestCase class in our testing class. In this example we are going to test the functionality of adding two numbers. So we have created our class named CalculatorTest. In this class there is one method to testSum to test addition functionality of the program. To make these methods of any use our class extends TestCase class.

public CalculatorTest(String name) {
             super(name);
     }

When we test the functionality then we can see the output to check which test has produced error i.e. which test fails. So for this every test is given name. The constructor of the class provides this functionality by passing this parameter to the constructor of the parent class.

public void testSum() {
             assertEquals(2,cal.sum(1,1));
    }

The method testSum() is to test the functionality of addition in the Calculator.java class. In this example, there is a need for only one method but we can add as many methods as we need. We can also define variables and perform arithmetic calculations just as we do in any Java program. In this method we are checking whether the value returned by sum method of the object of the Calculator class is equal to "2". If it is so then the test will be shown successful. If the method did not perform as expected then it will cause assertEquals() to fail. Now we need to fix the problem and run the test again . We need to repeat this process until the test is passed.

How to run JUnit in text mode :

Execute java junit.textui.TestRunner CalculatorTest. The passing test results in the following textual output:

.
Time: 0

OK (1 test)

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.