JUnit4.0 GouthamV Software Engineer InfoGroup
Introduction Features Uses Annotations Enhanced setup & tear down methods Ignore methods Timer settings Summary
Introduction Main aim is to make testing simpler by using new features of Java 5, esp: Annotations Gives more control over naming of the test case methods Maintains backwards compatibility with existing JUnit 3.8 test suites Encourages more developers to write more  tests by further simplifying existing JUnit code
Annotations: Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program Example usages are, naming the author of a piece of code or instructing the compiler to suppress specific errors An annotation has no effect on how the code performs Existing Annotations in JDK 1.4 and previous versions are  restricted to java doc comments, for example  /** * @author Gautam  * @param  element whose presence in this List is to be tested.  * @return  <code>true</code> if the specified element is present; */
Junit4 is purely based on customized Annotations, for example @Before, @BeforeClass, @Test, @After, @AfterClass etc Sample Annotations in JDK 5, @Deprecated, @Override, @SuppressWarnings These Annotation are available in java.lang package so it is not  required to mention import java.lang.Deprecated class @Deprecated annotation indicates that the marked method should no longer be used. Useful if we are changing functionality of a existing method and creating a new method to replace that @Override annotation informs the compiler that the element is meant to override an element declared in a super class The @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate
Examples: class  A{ protected void  method(){}   @Deprecated   static void deprecatedMethod() { } } class B extends A {   @Override    protected void  method() {  }   @SuppressWarnings(&quot;deprecation&quot;)   void useDeprecatedMethod() {   Animal.deprecateMethod(); //deprecation warning - suppressed }
JDK 5 supports  user defined Annotations, we can create our own  Annotation according to our needs, we can give more  meaningful  description to particular piece of a code User defined Annotations: public @interface CodeMigration { String DeveloperName (); String Description () default &quot;[unassigned]&quot;;  String date ();  default &quot;[unassigned]&quot;;  } @ CodeMigration ( DeveloperName = “Gautam” Description  = “Applying Juint4 features to test DAO    classes”   date  =  “3/22/2006“ ) public class ReasonCdsDAOTest {} public void loadTest() { ... } }
setUp() & tearDown() methods in Junit4: In JUnit 4, you can still initialize fields and configure the  environment before each  test method is run. However, the  method  that does it no longer needs to be called setUp(). it just needs to be  denoted with the @Before and  @After for tearDown() Supports inheritance  Use @BeforeClass, @AfterClass if each test in the class uses a  database connection, a network connection, a very large data  structure, or some other resource  that's expensive to initialize or  dispose of Use @Test for testcase methods instead of testMethod()  convention that is  adding “test”keyword before every method @Ignore method is for ignoring the @Test methods similar to applying  comments
Timer settings Use @Test(timeout=) Annotation for Benchmark testing. If you're not working on things that are likely to break that test, you might want to skip the long-running test method to speed up your compile-test-debug cycle. Or perhaps a test is failing for reasons beyond your control. Perhaps you have a test that takes an excessively long time to run If a remote host or database a test is trying to connect to  is down or slow, you can bypass that test so as not to hold up all the other tests (Source:http://www-128.ibm.com/developerworks/java/library/j-junit4.html)
Summary JUnit4 requires JDK 5 to run Test classes do not have to extend from TestCase Test methods do not have to be prefixed with ‘test’ Additional assert method in Junit4 is public static void assertEquals(String message, Object[] expected)  Use @Test annotations to mark a method as a test case  @Before and @After annotations take care of set up and tear down  @BeforeClass and @AfterClass annotations take care of one time set up and one time tear down (Source:http://www.instrumentalservices.com/index.php?option=com_content&task=view&id=45&Itemid=52)
  The End!

Junit4.0

  • 1.
    JUnit4.0 GouthamV SoftwareEngineer InfoGroup
  • 2.
    Introduction Features UsesAnnotations Enhanced setup & tear down methods Ignore methods Timer settings Summary
  • 3.
    Introduction Main aimis to make testing simpler by using new features of Java 5, esp: Annotations Gives more control over naming of the test case methods Maintains backwards compatibility with existing JUnit 3.8 test suites Encourages more developers to write more tests by further simplifying existing JUnit code
  • 4.
    Annotations: Release 5.0of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program Example usages are, naming the author of a piece of code or instructing the compiler to suppress specific errors An annotation has no effect on how the code performs Existing Annotations in JDK 1.4 and previous versions are restricted to java doc comments, for example /** * @author Gautam * @param element whose presence in this List is to be tested. * @return <code>true</code> if the specified element is present; */
  • 5.
    Junit4 is purelybased on customized Annotations, for example @Before, @BeforeClass, @Test, @After, @AfterClass etc Sample Annotations in JDK 5, @Deprecated, @Override, @SuppressWarnings These Annotation are available in java.lang package so it is not required to mention import java.lang.Deprecated class @Deprecated annotation indicates that the marked method should no longer be used. Useful if we are changing functionality of a existing method and creating a new method to replace that @Override annotation informs the compiler that the element is meant to override an element declared in a super class The @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate
  • 6.
    Examples: class A{ protected void method(){} @Deprecated static void deprecatedMethod() { } } class B extends A { @Override protected void method() { } @SuppressWarnings(&quot;deprecation&quot;) void useDeprecatedMethod() { Animal.deprecateMethod(); //deprecation warning - suppressed }
  • 7.
    JDK 5 supports user defined Annotations, we can create our own Annotation according to our needs, we can give more meaningful description to particular piece of a code User defined Annotations: public @interface CodeMigration { String DeveloperName (); String Description () default &quot;[unassigned]&quot;; String date (); default &quot;[unassigned]&quot;; } @ CodeMigration ( DeveloperName = “Gautam” Description = “Applying Juint4 features to test DAO classes” date = “3/22/2006“ ) public class ReasonCdsDAOTest {} public void loadTest() { ... } }
  • 8.
    setUp() & tearDown()methods in Junit4: In JUnit 4, you can still initialize fields and configure the environment before each test method is run. However, the method that does it no longer needs to be called setUp(). it just needs to be denoted with the @Before and @After for tearDown() Supports inheritance Use @BeforeClass, @AfterClass if each test in the class uses a database connection, a network connection, a very large data structure, or some other resource that's expensive to initialize or dispose of Use @Test for testcase methods instead of testMethod() convention that is adding “test”keyword before every method @Ignore method is for ignoring the @Test methods similar to applying comments
  • 9.
    Timer settings Use@Test(timeout=) Annotation for Benchmark testing. If you're not working on things that are likely to break that test, you might want to skip the long-running test method to speed up your compile-test-debug cycle. Or perhaps a test is failing for reasons beyond your control. Perhaps you have a test that takes an excessively long time to run If a remote host or database a test is trying to connect to is down or slow, you can bypass that test so as not to hold up all the other tests (Source:http://www-128.ibm.com/developerworks/java/library/j-junit4.html)
  • 10.
    Summary JUnit4 requiresJDK 5 to run Test classes do not have to extend from TestCase Test methods do not have to be prefixed with ‘test’ Additional assert method in Junit4 is public static void assertEquals(String message, Object[] expected) Use @Test annotations to mark a method as a test case @Before and @After annotations take care of set up and tear down @BeforeClass and @AfterClass annotations take care of one time set up and one time tear down (Source:http://www.instrumentalservices.com/index.php?option=com_content&task=view&id=45&Itemid=52)
  • 11.
    TheEnd!