Thursday, 5 December 2013

Advantages of TestNG Framework



TestNG is testing framework inspired from most popular JUnit framework used for the Java programming language. The TestNG framework is introduced to overcome the limitations of JUnit framework. Most of the automation users are using this framework because of its advantages & more supported features. Until we have executed selenium test script but not even generated test reports. So using this TestNG framework we will learn how to generate test reports.
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:
·        Annotations.
·        Flexible test configuration.
·        Support for data-driven testing (with @DataProvider).
·        Support for parameters.
·        Allows distribution of tests on slave machines.
·        Powerful execution model (no more TestSuite).
·        Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
·        Dependent methods for application server testing.
·        In TestNG Annotations are easy to understand over JUnit.
·        In TestNG there is no constraint like you have to declare @BeforeClass and AfterClass, which is present in JUnit.
·        As method name constraint is present in JUnit, such method name constraint is not present in TestNG and you can specify any test method names.
·        In TestNG enable you to grouping of test cases easily which is not possible in JUnit.
·        TestNG supports following three 3 additional setUp/tearDown level:
·        Before/AfterSuite, Before/AfterTest and Before/AfterGroup.
·        TestNG do not require extend any class.
·        TestNG allows us to define the dependent test cases each test case is independent to other test case.
·        TestNG allows us to execute of test cases based on group. Let’s take a scenario where we have created two set of groups “Regression” & “Sanity”. If we want to execute the test cases under Sanity group then it is possible in TestNG framework.
·        Parallel execution of Selenium test cases is possible in TestNG.

Wednesday, 4 December 2013

Java object example, How to create object in java.




In this example, we will see how to create a object in java. An object is a main feature of object oriented programming concept. It is an instance variable of class or real-world entity. We can declare a object by using new operator. The new operator dynamically allocate memory of object and returns a reference for object.

Here, a ObjectExample class is a supper class of ObjectDemo class. Subclass has main method, and also supper class has a show() method. Which is use for displaying a string "Hello world.". The obDemo is a object of supper class.

Syntax

ClassName object=new ClassName();
ClassName object (Reference of object)
ClassName object=new ClassName(); (allocation)

Code:
ObjectDemo.java

package com.devmanuals;

class ObjectExample {
        public void show() {
                System.out.println("Hello world.");
        }
}
public class ObjectDemo {
        public static void main(String[] arg) {
                ObjectExample obDemo = new ObjectExample();
                obDemo.show();
        }
}
Output:
Hello world.

Tuesday, 3 December 2013

Read interactive command-line input with Java



While Java is generally used to create applets, servlets, and general-purpose applications, you may occasionally need to create applications that interactively communicate with a user at a command-line prompt, such as a Unix or DOS prompt. In these cases you'll see a prompt that looks something like this:
Enter your name: _
Unfortunately, this is one area where it's difficult to use vanilla Java methods. If you're used to simple echo and read statements in shell scripts, you're in for a bit of shock. I suspect the creators of Java didn't expect to see their language used for this purpose, or they just assumed developers would create their own classes to simplify this process.
 we'll present a technique we use when creating Java programs that require interactive command-line input. If you like this basic technique, and would like to see expanded coverage of how to read numeric values, or would like to see a custom class for handling command-line input, just leave a comment below, and we'll provide a follow-up as quickly as we can.
The basic technique of reading a String provided by a user at a command-line is fairly simple, but more lengthy than you'd expect. It involves the use of the System.in object, along with the InputStreamReader and BufferedReader classes. (See my Java BufferedReader examples for more information on those classes.)

The code shows how you can prompt the user to enter a String value, such as their name, and then read that value:
import java.io.*;

public class ReadString {

   public static void main (String[] args) {

      //  prompt the user to enter their name
      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String userName = null;

      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         userName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your name!");
         System.exit(1);
      }

      System.out.println("Thanks for the name, " + userName);

   }

}  // end of ReadString class
1The ReadString.java program shows how you can prompt the user to enter their name, and then read the name into the userName variable.

1 demonstrates how you can print a prompt to the user using the System.out.print() method. Notice that we use the print() method instead of println(). Using the print() method lets us keep the cursor on the same line of output as our printed text. This makes it look like a real prompt (instead of having the user's response appear on the line below our prompt).

Next, we read the user's input by passing the System.in object to the InputStreamReader and then into the BufferedReader The Java BufferedReader class gives us the readLine() method, and applies buffering to the input character input stream. Notice that the readLine() method can thrown an IOException error, so we have to enclose the statement in a try/catch statement.





Monday, 2 December 2013

Use Equals methods for comparing String in Java



String class overrides equals method and provides a content equality, which is based on characters, case and order. So if you want to compare two String object, to check whether they are same or not, always use equals() method instead of equality operator. Like in earlier example if  we use equals method to compare objects, they will be equal to each other because they all contains same contents. Here is example of comparing String using equals method.

String name = "Java"; //1st String object
String name_1 = "Java"; //same object referenced by name variable
String name_2 = new String("Java") //different String object

if(name.equals(name_1)){
System.out.println("name and name_1 are equal String by equals method");
}

//this will return false
if(name==name_2){
System.out.println("name_1 and name_2 are equal String by equals method");
}


Difference between == and equals method in Java

Now we know what is equals method, how it works and What is equality operator (==) and How it compare objects, its time to compare them. Here is some worth noting difference between equals() method and == operator in Java:

·          First difference between them is, equals() is a method defined inside the java.lang.Object class and == is one type of operator and you can compare both primitive and objects using equality operator in Java.

·          Second difference between equals and == operator is that, == is used to check reference or memory address of the objects whether they point to same location or not, and equals() method is used to compare the contents of the object e.g. in case of comparing String its characters, in case of Integer its there numeric values etc. You can define your own equals method for domain object as per business rules e.g. two Employes objects are equal if there EmployeeId is same.

·          Third difference between equals and == operator is that, You can not change the behavior of == operator but we can override equals() method and define the criteria for the objects equality.

Let clear all these differences between equals and == operator using one Java example :

String s1=new String("hello");
String s2=new String("hello");


Here we have created two string s1 and s2 now will use == and equals () method to compare these two String to check whether they are equal or not.