Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


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