Showing posts with label Advantage and Disadvantage of Reflection API. Show all posts
Showing posts with label Advantage and Disadvantage of Reflection API. Show all posts

Friday, August 29, 2014

JUnit Assertions

Assertions is one of the most used and important part of your Framework
JUnit provides overloaded Assertions for all primitive types(ie int, string etc), Objects and Arrays(Primitive and Objects)

Syntax for Assertions
<expected_value> , <actual_value>
or
<String output for failure>, <expected_value> , <actual_value>

Examples

1)Compares if text are same
 @Test
  public void testAssertEquals() {
    org.junit.Assert.assertEquals("failure - strings are not equal", "text", "text");
  }

2)Checks if condition passed results in false, if false_var is evaluated to true then error is shown
Remember this condition can be used to optionally run your test case, if you include this in your @Before Class

  @Test
  public void testAssertFalse() {
    org.junit.Assert.assertFalse("failure - should be false", false_var);
  }

3)Checks if the obect passed is null or not, if not null, then shows error
   @Test
  public void testAssertNotNull() {
    org.junit.Assert.assertNotNull("should not be null", new Object());
  }

4)Checks if two obects are same
  @Test
  public void testAssertNotSame() {
    org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());
  }

A slightly different assertion is assertThat, usage is

Syntax
<optional_failure_msg> , <actual_value> , <matcher_Object>

asserThat is used in the suitations, where the actual value is dynamically generated and you can use a matcher obect to verify it at run time with a pattern

Examples

1) This can be used to check your order_num pattern, date etc
 @Test
  public void testAssertThatBothContainsString() {
    org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
  }

2)Checks the Array List

  @Test
  public void testAssertThatEveryItemContainsString() {
    org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
  }

  

Monday, August 25, 2014

What is Reflection API? Advantages of Reflection API and Disadvantages of Reflection API

The Reflection API allows Java code to examine the classes and objects at run time.
The new Reflection classes allows you to call another class methods dynamically a the run time

EXAMPLE

Say you have an object of unknown type in java, you would like to call a method, dosomething on it if it exists

Static typing languages isn't really designed to support this, unless the object conforms to a known interface. But with reflection your code can look at the object and find if it has a method called dosomething and then call if it want to

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class Learn_reflection {


public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {


Learn_reflection t = new Learn_reflection();
Method met[] = t.getClass().getMethods();

for(int i = 0; i<met.length; i++){
if(met[i].getName().equals("Test1")){
met[i].invoke(t);
}
}
}

public void Test1() {
System.out.println("Test1");
}

public void Test2() {
System.out.println("Test2");
}

}


Advantages of Reflection API
1) Helps to look at the methods and objects at run time

DisAdvantages of Reflection API
1) Performance overload, since everything is dynamically resolved, JVM may not be able to perform certain optimizations
2)Security Restrictions : Refecion requires runtime permission to the code, which might not always be allowed