Sunday, 31 March 2013

Serializing Child with Parent not Serializable

This is interesting to see when you serialize a child which has one of the parent not serializable, on deserialization constructor of that parent class gets called.

In Given example while deserialization constructor of GrandParent will be called.

Note: While deserialization it calls only the default constructor of such parent. If non-serializable parent lack default constructor it will throw exception for "no valid constructor".

class Grandparent {
// must need default constructor
    public Grandparent() {
        System.out.println("A");
    }
}

 class Parent extends Grandparent implements Serializable {
    public Parent() {
        System.out.println("B");
    }
}
 class Child extends Parent {
    public Child() {
        System.out.println("C");
    }
}


public class TestSerialization{
    public static void main(String args[]) throws IOException, ClassNotFoundException {
        System.out.println("Serialization ....");
        ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("/object"));
        out.writeObject(new Child());
        System.out.println("Now Deserialization ....");
        ObjectInputStream in = new ObjectInputStream( new FileInputStream("/object"));
        Object obj = in.readObject();
        System.out.print(obj + " -- " + obj.getClass());
    }
}

OUTPUT
----------


Serialization ....
A
B
C
Now Deserialization ....
A
co.uk.sample.Child@d2906a -- class co.uk.sample.Child



Monday, 11 March 2013

TestNG vs JUnit


Here are some features of TestNG which is not available in Junit
1. Method Dependency: In Junit execution of test methods is depends on order of string literals in method name. Its hard to maintain the ordering and its not visible if there is any dependency of one method on other method. On the Other hand in TestNG you can mention dependency using dependsOnMethods.
@Test ( dependsOnMethods = "testPreSettlement" )
public void testSettlement(){ ... }

In this case if first method fails, second method doesn't go in fail status but you will see it as skipped.

2. Parametric Testing (DDT): As data driven test framework TestNG  has feature of parametric testing where you can pass parameter to a test method using configuration. Junit needs more code to use this feature.
@testng.parameters value="param1, param2"
public void testSettlement(){ ... }

You can configure this in test suite file as below:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd">
<suite name="settlement">
 <test name="testSettlement">
  <parameter name="param1" value="Admin"/>
  <parameter name="param2" value="2"/>
  <classes> 
   <class name="co.uk.SettlementTest"/>
  </classes>
 </test> 
</suite>

3. Ordering/Prioritiesing Test cases: In TestNG you can use Priority annotation to put ordering between test cases.
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Priority { int value() default 0; }

For Example
@Priority(-1)
@Test
public void testSettlement(){ ... }

4. Grouping Tests and Repeatability:  In TestNg you can group tests, even configure number of times you want to execute particular test case. You can also set timeout for executing test method.
@Test(threadPoolSize = 4, invocationCount = 5,  timeOut = 10000, groups = { "functional"})