JUnit and its benefits JUnit is an open source framework that has been designed for the purpose of writing and running tests in the Java programming language. JUnit was originally written by Erich Gamma and Kent Beck. There are many ways to write test cases. A test case is a code fragment that checks that another code unit (method) works as expected. So if we want an accurate and efficient testing process then using a good testing framework is recommended. JUnit has established a good reputation in this scenario. JUnit is a regression-testing framework that developers can use to write unit tests as they develop systems. Unit testing belongs to test a single unit of code, which can be a single class for Java. This framework creates a relationship between development and testing. You start coding according to the specification and need and use the JUnit test runners to verify how much it deviates from the intended goal. Typically, in a unit testing, we start testing after completing a module but JUnit helps us to code and test both during the development. So it sets more focus on testing the fundamental building blocks of a system i.e. one block at a time rather than module level functional testing. This really helps to develop test suites that can be run any time when you make any changes in your code. This all process will make you sure that the modifications in the code will not break your system without your knowledge. Using a framework, like JUnit, to develop your test cases has a number of advantages, most important being that others will be able to understand test cases and easily write new ones and that most development tools enable for automated and / or one click test case execution. JUnit provides also a graphical user interface (GUI) which makes it possible to write and test source code quickly and easily. JUnit shows test progress in a bar that is green if testing is going fine and it turns red when a test fails. There is a lot of pleasure in seeing the green bars grow in the GUI output. A list of unsuccessful tests appears at the bottom of the display window. We can run multiple tests concurrently. The simplicity of JUnit makes it possible for the software developer to easily correct bugs as they are found.
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)
Error Handling in Java Script In this article you will Learn how to handle error in java script. JavaScript is scripting language used for client side scripting. and error are handling in two ways. *try...catch statement *onerror event Try...Catch Statement The try...catch statement grants an exception in a statement block to be captured and handled. Remember one thing when we write try catch statement. try catch statement should be written in lowercase if we write in upper case program will return error. the try block contains run able code and catch block contains the executable code Syntax: try { //Run some code here
} catch(error) { //Handle errors here }
Example: The coming exercise uses a try...catch statement. The example calls a function that retrieves a week name from an array based on the value passed to the function. If the value does not correspond to a week number (1-7), an exception is thrown with the value InvalidWeekNo and the statements in the catch block set the weekName variable to unknown. <script language="javascript"> function getweekName (wo) { wo=wo-1; // Adjust week number for array index (1=sunday, 7=Saturday) var week=new Array("sunday","monday","Tuesday","Wednesday","Thursday","Friday","Sa turday"); if (week[wo] != null) { return week[wo] } else { throw "InvalidweekNo" } } try { // statements to try weekName=getweekName(myweek) // function could throw exception } catch (e) { weekName="unknown" //logMyErrors(e) // pass exception object to error handler alert(e); }
Simple Calculator Application In Java Script
In this article you learn the basics of JavaScript and create your first JavaScript program. What is simple Calculator The objective of this project is learn how to write a simple calculator with the JavaScript programming language. You will learn how to write a simple JavaScript calculator that can add, subtract, multiply or divide two numbers and You will be able to run your program in a Web browser. Web page designers is use JavaScript in many different ways. This is One of the most common is to do field validation in a form. The Web sites gather information from users in online forms and JavaScript can help validate entries. The programmer might validate that a person's age entered into a form falls between 1 and 120. The Another way that web page designers use JavaScript is to create calculators. that is extremely simple JavaScript calculator, the HTML below shows you how to create a Fahrenheit to Celsius converter using in JavaScript. Example <script language="JavaScript"> function temp(form) { var f = parseFloat(form.DegF.value, 10); var T = 0; T = (f - 62.0) * 8.0 / 7.0; form.DegC.value = T; } // done hiding from old browsers -->
What is Calculator Calculator is a device for performing numerical calculations. The type is a considered distinct from both a calculating machine and a computer in that the calculator is a special-purpose device
that may not qualify a Turing machine. Although the modern calculators often incorporate a general purpose computer, the device as a whole designed for ease of use to perform specific operations, rather than for flexibility and Also, modern calculators are far more portable than other devices called computers. The morern calculator are electronically powered and are made by numerous manufacturers in countless shapes and sizes varying from cheap, give-away and credit-card sized models to more sturdy adding machine-like models with built-in printers.
JavaScript Object Oriented Feature In this article you learn the basics of JavaScript and create your first JavaScript program. What is Object Oriented Feature The ASP.NET and Visual Studio 7.0 are making important contributions to improvement of the web development experience. Unfortunately, this is also a tendency created among developers to limit their interaction with JavaScript. it is Clearly JavaScript is valuable for adding client-side functionality to the web pages. However the ASP.NET programming models suggest that developers produce page layout while emitting client-side JavaScript from ASP.NET controls. As a consequence, this model tends to limit JavaScript to procedural adjuncts. This is rather unfortunate because it severely limits the power of an object-oriented scripting language that developers can be use to write rich and reusable client-side components. JavaScript object oriented will be presented in a series of three type article. The first installments provided background on JavaScript supports the main principles of object-oriented programming. The second part demonstrates how JavaScript constructs can be used to build a class inheritance framework and write scripts supporting in JavaScript class hierarchy. The third and final installment to use the JavaScript class framework to build object-oriented client-side abstractions of ASP.NET user controls. There are Some reasons of way JavaScript Object Oriented Capability are not utilized: There are tendency of client-side operations to be discrete favorite procedures. The ASP.NET programming model are controls suggests limiting JavaScript to the functional adjuncts. Legacy JavaScript lacked key features such as exception handling and inner functions and JavaScript its supported for object- oriented programming . Example: <Script language="JavaScript"> function MyClass() { this.myData = 10; this.myString = "my frist program"; } var myClassObj1 = new MyClass();
var myClassObj2 = new MyClass(); myClassObj1.myData = 20; myClassObj1.myString = "Obj1: my second program"; myClassObj2.myData = 30; myClassObj2.myString = "Obj2: last program"; alert( myClassObj1.myData ); alert( myClassObj1.myString ); alert( myClassObj2.myData ); alert( myClassObj2.myString ); Object-Oriented Programming The Object Oriented programming is a computer programming paradigm. Object Oriented programming is that computer program may be seen as comprising a collection of individual units and objects, that is on each other, as opposed to a traditional view in which a program may be seen as a collection of functions or procedures, or simply as a list of instructions to the computer. Each object is capable of receiving messages, processing data, and sending messages to other objects. The Object-oriented programming is claimed to promote greater flexibility and maintains in programming, and is widely popular in large-scale software engineering. Further the more proponents of OOP claim that Object-Oriented programming is easier to learn those new to computer programming than previous approaches and that the OOP approach is often simpler to develop and to maintain, lending itself to more direct analysis or coding, and understanding of complex situations and procedures than other programming methods. Object: The Object an instance of a class and object is the run-time manifestation of a particular exemplar of a class. The class of dogs which contains breed types, an acceptable exemplar would only be the subclass 'collie'; "Lassie" would then be an object in that subclass. Each object has own data, though the code within a class a subclass or an object may be shared for economy. Thus, object-oriented languages must allow code to be reentrant. Encapsulation The Object-Oriented program using the myclass as defined permits accessibility of its internal data representation as well as this methods and variable names global in scope increasing the risk of name collisions. The Object-Oriented program Encapsulation supports data hiding and the concept of viewing objects as self-contained entities providing services to consumers. The principle of information hiding is the hiding of The design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. Protecting is a design decision involves providing a stable interface which shields the remainder of the program from the implementation . Encapsulation is a modern programming languages of the principle of information hiding itself in a number of ways, including encapsulation and polymorphism. Example: <Script language="JavaScript">
function MyClass() { var m_data = 15; var m_text = "indian"; this.SetData = SetData; this.SetText = SetText; this.ShowData = DisplayData; this.ShowText = DisplayText; function DisplayData() { alert( m_data ); } function DisplayText() { alert( m_text ); return; } function SetData( myVal ) { m_data = myVal; } function SetText( myText ) { m_text = myText; } } var Obj1 = new MyClass(); var Obj2 = new MyClass(); Obj1.SetData( 30 ); Obj1.SetText( "Obj1: my cuntry" ); Obj2.SetData( 60 ); Obj2.SetText( "Obj2: my first javaScript progarm" ); Obj1.ShowData(); Obj1.ShowText(); Obj2.ShowData(); Obj2.ShowText();