Wednesday, 22 January 2014

Static Nested Classes



Static member class, also called static nested classes- They are declared static. Like other things in static scope (i.e. static methods), they do not have an enclosing instance, and cannot access instance variables and methods of the enclosing class. They are almost identical to non-nested classes except for scope details (they can refer to static variables and methods of the enclosing class without qualifying the name; other classes that are not one of its enclosing classes have to qualify its name with its enclosing class's name). Nested interfaces are implicitly static.

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass {
    ...
    class InnerClass {
        ...
    }
}

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();
There are two special kinds of inner classes: local classes and anonymous classes.
Nested static class is another class which is declared inside a class as member and made static. Nested static class is also declared as member of outer class and can be make private, public or protected like any other member. One of the main benefit of nested static class over inner class is that instance of nested static class is not attached to any enclosing instance of Outer class. You also don't need any instance of Outer class to create instance of nested static class in Java. This makes nested static class very convenient to use and access.

Here is an example of nested static class in Java. It look exactly similar to member inner classes but has quite a few significant difference with them, e.g. you can access them inside main method because they are static. In order to create instance of nested static class, you don’t need instance of enclosing class. You can refer them with class name and you can also import them using static import feature of Java 5.

public class NestedStaticExample {

    public static void main(String args[]){
 
        StaticNested nested = new StaticNested();
        nested.name();
    }
 
    //static nested class in java
    private static class StaticNested{
        public void name(){
            System.out.println("static nested class example in java");
        }
    }
}



Monday, 20 January 2014

Object identification in selenium ide


For most applications, easily use the built in Selenium IDE in Firefox
In Firefox, click on Tools>Selenium IDE.
Make sure the red record button is in the “Now recording..” mode.
Notice how the Selenium IDE automatically recognizes what information (id-tools) to use to identify the Select tool dropdown.
Click on the select command to highlight it, then click the “FIND’” button.
we will understand how Selenium identifies the objects on the Application Under Test.
Every page is HTML. While recording, when we click on some element, IDE selects a particular property of that element that is unique in that page. Hence it is able to perform the operations

To identify the objects such as Links, Buttons, Edit boxes, Drop downs, etc on the application Selenium uses a concept called “Locators”.  There are around 8 different types of locators.  Out of them, I will be explaining only four as they are widely used while automating the test cases using Selenium.

Every Web Page will be having some HTML Source code.  This can be viewed using “View –> Page Source / View source” on the browser. In the following picture we can see “id” attribute of a HTML tag is used as a locator to identify an object.


ü  Login to sample application
ü  Click on Accounts tab
ü  Click on Create Account link
ü  Now Create Account Page gets displayed
ü  Selenium IDE has recorded all these steps
3)  xpath = xpathExpression.  Xpath is used as a Locator to identify an object.  This is an expression which is formed by combining various HTML tags
4)   link=link text (in HTML source we can located this using “href” tag)

Friday, 17 January 2014

Testing Using JUnit


TestingUsing JUnit:
While compilers can look for structural problems in a program, they cannot tell whether the results of a program or method are correct. Instead, all developers test their programs to ensure that they behave as expected.
This can be as simple as calling a method in the Interactions Pane to view its results, but this technique requires you to think about the answers you expect every time you run any tests. A much better solution is to give the tests the answers you expect, and let the tests themselves do all the work.
a technique known as unit testing makes this quite easy. You write many small tests that create your objects and assert that they behave the way you expect in different situations. A unit test framework known as JUnit automates the process of running these tests, letting you quickly see whether your program returns the results you expect.
Java makes the process of running unit tests very simple by providing support for JUnit. Once you have written a JUnit test class (as described in the next section), you can simply choose the "Test Current Document" command from the Tools menu to run the tests and view the results.
Also, clicking the "Test" button on the toolbar or choosing "Test All Documents" from the Tools menu will run JUnit on any open testcases, making running multiple test files very simple.
JUnit is a framework for developing unit tests for Java classes. JUnit provides a base class called TestCase that can be extended to create a series of tests for the class you are creating, an assertion library used for evaluating the results of individual tests, and several applications that run the tests you create.

With the JUnit framework, unit tests are any public classes that extend the junit.framework.TestCase class, and that have any number of methods with names beginning with the word "test". JUnit provides methods to easily assert things about your own classes, as well as the ability to run a group of tests.

The requirements for writing unit test classes are described below, with an example provided in the next section. In general, though, the intent is for you to create instances of your classes in the test methods, get results from any methods that you call, and assert that those results match your expectations. If these assertions pass, then your program has behaved correctly and your tests have succeeded.

Writing a Test Case. To use  Java's Test command on a document, you must use the programming conventions outlined below. You can also choose the "New JUnit Test Case" command from the File menu to automatically generate a template with these conventions.
At the top of the file, include:

import junit.framework.TestCase;
The main class of the file must:

be public

extend TestCase


be public and not static

return void

take no arguments

have a name beginning with "test"


void assertTrue(String, boolean)
which issues an error report with the given string if the boolean is false.

void assertEquals(String, int, int)
which issues an error report with the given string if the two integers are not equal. The first int is the expected value, and the second int is the actual (tested) value. Note that this method can also be called using any primitives or with Objects, using their equals() methods for comparison.

void fail(String)

Test methods are permitted to throw any type of exception, as long as it is declared in the "throws" clause of the method contract. If an exception is thrown, the test fails immediately.

If there is any common setup work to be done before running each test (such as initializing instance variables), do it in the body of a method with the following contract:

This method is automatically run before any tests in the class. (Similarly, you can write a protected void tearDown() method to be called after each test.)

If you would rather control which methods are called when running the tests (rather than using all methods starting with "test"), you can write a method to create a test suite. This method should be of the form:

public static Test suite() {
  TestSuite suite = new TestSuite();
  suite.addTest(new <classname>("<methodname>"));
  ...
  return suite;
}
It is then also necessary to import TestSuite and Test from junit.framework. There is also a version of the addTest method that takes a Test, so test suites can be composed.


Friday, 10 January 2014

JUnit Overview




JUnit is a unit testing framework for the Java Programming Language. It is important in the test driven development, and is one of a family of unit testing frameworks collectively known as xUnit.

JUnit promotes the idea of "first testing then coding", which emphasis on setting up the test data for a piece of code which can be tested first and then can be implemented . This approach is like "test a little, code a little, test a little, code a little..." which increases programmer productivity and stability of program code that reduces programmer stress and the time spent on debugging.
JUnit is an open source framework and for the purpose of writing and running test cases for java programs. In the case of web applications JUnit is used to test the application with out server. This framework builds a relationship between development and testing process.


JUnit is an open source framework which is used for writing & running tests.

Provides Annotation to identify the test methods.

Provides Assertions for testing expected results.

Provides Test runners for running tests.

JUnit tests allow you to write code faster which increasing quality

JUnit is elegantly simple. It is less complex & takes less time.

JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.

JUnit tests can be organized into test suites containing test cases and even other test suites.

Junit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.


A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected. To achieve those desired results quickly, test framework is required .JUnit is perfect unit test framework for java programming language.

A formal written unit test case is characterized by a known input and by an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a postcondition.

There must be at least two unit test cases for each requirement: one positive test and one negative test. If a requirement has sub-requirements, each sub-requirement must have at least two test cases as positive and negative.



If we want to test any application, In standalone, we need to call the respective method’s main() method but there are so many problems in testing the flow using main() method.

In a web application, to test the flow we need to deploy it in the server so if there is a change then again we need to restart the server. Here also facing problems while testing the application.

Writing main method checks is convenient because all Java IDEs provide the ability to compile and run the class in the current buffer, and certainly have their place for exercising an object’s capability. There are, however, some issues with this approach that make it ineffective as a comprehensive test framework:
There is no explicit concept of a test passing or failing. Typically, the program outputs messages simply with System.out.println; the user has to look at this and decide if it is correct.
main has access to protected and private members and methods. While you may want to test the inner workings of a class may be desired, many tests are really about testing an object’s interface to the outside world.
There is no mechanism to collect results in a structured fashion.
There is no replicability. After each test run, a person has to examine and interpret the results.
The JUnit framework addresses these issues, and more


JUnit addresses the above issues, It’s used to test an existing class. Suppose, If we have Calculation class then, we need to write CalculationTest to test the Calculation class.
Using JUnit we can save testing time.
In real time, in the web application development we implement JUnit test cases in the DAO classes. DAO classes can be tested with out server.
With JUnit we can also test Struts / Spring applications but most of the time we test only DAO classes. Even some times service classes also tested using JUnit.
Using JUnit, If we want to test the application (for web applications) then server is not required so the testing becomes fast.