Testing Options in Java


Published on

A J2EE Testing presentation I did for RMOUG, Aug2007 Rmoug 2nd Qty Mtg

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

Testing Options in Java

  1. 1. Testing Options in Oracle ADF Michael A. Fons Graebel Companies, Inc. August 2007
  2. 2. Introduction <ul><li>I have been working as an Oracle developer for 17 years. </li></ul><ul><ul><li>My daily work has mostly involved PL/SQL, web technolgies, Forms, Reports, … </li></ul></ul><ul><li>Java in my career </li></ul><ul><ul><li>I have spent off-hours for the last 2.5 years working with JDeveloper and ADF (and have tried to get more involved with Java technologies for the last 7 years). </li></ul></ul><ul><ul><li>Nevertheless, J2EE testing and development methodologies sometimes feel foreign to me. </li></ul></ul><ul><li>For this presentation… </li></ul><ul><ul><li>I have spent the last 3 months (after work) trying to learn all I can about testing Java programs and ADF applications. </li></ul></ul><ul><ul><li>I hope my perspective helps many people in this transitional time for Oracle developers. </li></ul></ul>
  3. 3. Outline <ul><li>Topics </li></ul><ul><ul><li>What does Oracle offer to test ADF/J2EE app? </li></ul></ul><ul><ul><li>Different sorts of products available to help test? </li></ul></ul><ul><li>This presentation is a survey of some software products which work (or not) with ADF Faces/JSF/ADF BC </li></ul>
  4. 4. Planned Demos (using JDev <ul><li>Test some model components using JUnit test cases. </li></ul><ul><li>Run a JUnit test script in using Ant automation. </li></ul><ul><li>Some “view-” (container-) level (black box/end-to-end) testing </li></ul><ul><li>Some other test products that work with ADF, for different testing purposes. </li></ul>
  5. 5. Testing an ADF Application <ul><li>Research J2EE testing types </li></ul><ul><ul><li>Unit/Object/Code Logic testing – is the app built correctly? [1] (It is a best practice to provide a test class for every class you develop for a project.) </li></ul></ul><ul><ul><li>End-to-end/Functional testing – did you build the right features into your app to satisfy the end users’ need(s)? [1] </li></ul></ul><ul><ul><li>Integration (with J2EE container) testing </li></ul></ul><ul><ul><li>Regression/System Integration testing. </li></ul></ul><ul><ul><li>Load/Speed testing </li></ul></ul>
  6. 6. JDeveloper testing basics <ul><li>JDeveloper primarily offers and promotes testing of the model layer using… </li></ul><ul><ul><li>JUnit – to run test scenarios on model components </li></ul></ul><ul><ul><li>Ant – to help automate testing runs </li></ul></ul><ul><li>Another feature you can use for model level testing includes… </li></ul><ul><ul><li>ADF has easy read/write of (test) bulk data using XML. (ViewObjects and Rows have readXML and writeXML methods.) </li></ul></ul>
  7. 7. JUnit basics <ul><li>What is JUnit? </li></ul><ul><ul><li>A basic unit test framework for Java (hence, “J”-Unit). </li></ul></ul><ul><ul><li>Very popular; very widely ported </li></ul></ul><ul><ul><ul><li>C#, </li></ul></ul></ul><ul><ul><ul><li>Python, </li></ul></ul></ul><ul><ul><ul><li>Fortran, </li></ul></ul></ul><ul><ul><ul><li>Perl, </li></ul></ul></ul><ul><ul><ul><li>C++, </li></ul></ul></ul><ul><ul><ul><li>JavaScript [4] </li></ul></ul></ul><ul><ul><li>Many sources referenced XP/XM (eXtreme Programming/Modeling in conjunction with JUnit) </li></ul></ul><ul><ul><li>Often the foundation of many other testing programs/paradigms </li></ul></ul>
  8. 8. JUnit basics (continued) <ul><li>Components </li></ul><ul><ul><li>TestCase -- it is a class you can extend which contains methods which test for particular business rule being implemented effectively or not. </li></ul></ul><ul><ul><li>TestSuite – is a class which runs a set of TestCases. (these cases can be specified explicitly or auto-detected) </li></ul></ul><ul><ul><li>TestFixture -- [9] “…a set of objects, having known values, that provide data for the test cases. Any class can serve as a test fixture…”. In this case, JDeveloper TestSuite wizard create a test fixture that gets the Application Module you need to test your ADF BC data. </li></ul></ul>
  9. 9. Ant basics <ul><li>Head, Thorax, Abdomen, six legs…no…not that kind of ant… </li></ul><ul><li>Ant is like “make”…only better. (Dependency management, XML-based, extendable, etc.) </li></ul><ul><li>Ant is often used in automating compiling programs. </li></ul><ul><li>In this context of this presentation, it automates batch processing JUnit Tests. </li></ul><ul><li>JDeveloper can create (Under General  Ant  Buildfile from Project) an Ant buildfile. </li></ul><ul><ul><li>Still needs a lot of work, though, to get it working with JUnit and an ADF app. </li></ul></ul><ul><ul><li>Best to emulate SRDemo build.xml file. </li></ul></ul><ul><li>When your ant build.xml (buildScript) is integrated with JUnit and running correctly for you…really neat log of testing results. </li></ul>
  10. 10. JUnit/Ant Installation <ul><li>Run “Check for updates”, under Help menu. </li></ul><ul><ul><li>Will need to login using Technet username and password </li></ul></ul><ul><ul><li>Download and install anything having to do with Ant or JUnit (include the extensions downloads catagory). </li></ul></ul><ul><li>Installing Ant will update your DOS PATH variable (so you will be able to run Ant in a “batch” file). </li></ul><ul><li>Type “ant -?” on command line for some help. </li></ul><ul><li>You can learn more, when you set Ant options (in or out of JDev) to be in “verbose” or “debug” mode. </li></ul><ul><li>Perhaps better to kick off Ant jobs from within JDeveloper; much less complex than maintaining a command file. </li></ul><ul><li>I do not yet know of a simple way to see what you would need to run on a command line to run this job in the middle of the night, say. I experimented with –lib switch, but event then there was still something missing. </li></ul>
  11. 11. JUnit TestCase Class <ul><li>To create a test case, extend JUnit TestCase class </li></ul><ul><li>Contains many “assert*” methods (and also “fail”). </li></ul><ul><ul><li>Examples: </li></ul></ul><ul><ul><ul><li>assertEquals – must find equality between first parameter and second or assertion fails. </li></ul></ul></ul><ul><ul><ul><li>assertTrue – boolean value of parameter must be true or the assertion fails </li></ul></ul></ul><ul><li>Test methods’ </li></ul><ul><ul><li>names must have naming convention “test*” </li></ul></ul><ul><ul><li>If any assert* calls in this method fail, then the outcome of your test method is failure as well. </li></ul></ul><ul><ul><li>If all assert* calls succeed, then the outcome of your test is success. </li></ul></ul>
  12. 12. JUnit: To Test any Pre-existing Method… <ul><li>Select the Java program in Application Navigator </li></ul><ul><li>Select method to test in Structure Pane. </li></ul><ul><li>Right-click to get context menu. </li></ul><ul><li>Click on “New Method test…” </li></ul><ul><li>Select Create or Add to TestCase Class; fill in other fields as appropriate and submit your changes. </li></ul><ul><li>(Note: this does not work with a constructor “method”; “New Method test…” will not be on the context menu in this case.) </li></ul>
  13. 13. JUnit: Create a TestCase for a Method <ul><li>Right-click on the project the method’s class is in. </li></ul><ul><li>Click New… </li></ul><ul><li>Under the General category, select the Test Case item, and click OK. </li></ul><ul><li>In the wizard, </li></ul><ul><ul><li>Page 1 (optional), either select your class from the dropdown list, or use the Browse… button to find your class in the appropriate package hierarchy. </li></ul></ul><ul><ul><li>Page 2, adjust field values as needed. Be careful about the package name; JDev seems to prefix your current package with “test.”. If you do not notice this you will have to refactor (move) your test case to get it into the package you intended. </li></ul></ul><ul><ul><li>Page 3 (optional), specify a test fixture if needed. </li></ul></ul>
  14. 14. Running a Test Case <ul><li>In JDeveloper you can run this test case class using the Run… context (right-click) menu command. </li></ul><ul><ul><li>This opens a JUnit Test Runner Window in JDeveloper; </li></ul></ul><ul><ul><li>your TestCase(s) are then run, </li></ul></ul><ul><ul><li>and the results are shown in the window. </li></ul></ul>
  15. 15. Demo 1 <ul><li>Test some model components using JUnit test cases (using test-first approach). </li></ul><ul><ul><li>Create a TestSuite for an application </li></ul></ul><ul><ul><li>Add a test case for an unimplemented business rule to one of the TestCase classes. </li></ul></ul><ul><ul><li>Run the test; watch the failure. </li></ul></ul><ul><ul><li>Implement the business rule. </li></ul></ul><ul><ul><li>Run the test again; watch it succeed. </li></ul></ul>
  16. 16. Demo 2 <ul><li>Run a JUnit test script in using Ant automation. </li></ul><ul><ul><li>This was adapted from SRDemo. </li></ul></ul><ul><ul><li>If you need a copy of the Ant build script so you can adapt it, email me (mfons@graebel.com). Or you can study SRDemo’s…somewhat challenging, for me. </li></ul></ul><ul><ul><li>Be careful about the way they define ${jdeveloper.home}. It appears that in some places in the build this does not translate and causes the test build not to run. </li></ul></ul><ul><ul><li>Command line: ??? seems difficult… </li></ul></ul><ul><ul><li>Cool output log in output directory. </li></ul></ul>
  17. 17. Testing tools… <ul><li>Fit (command line)/FitNesse (GUI interface) -- claims “collaboration and communication” through color-coded presentation of testing results – comparing customer’s expecte results with your program’s actual results; implemented on a variety of platforms [12] </li></ul><ul><li>StrutsTestCase – tests outside the app server with mock-objects </li></ul><ul><li>ServletUnit – simulates a Servlet container; alternative to mock-objects. </li></ul><ul><li>XMLUnit – tests different aspects of XML files. </li></ul><ul><li>Cactus – tests inside Servlet container </li></ul>
  18. 18. …more tools… <ul><li>More tools </li></ul><ul><ul><li>HtmlUnit (GargoyleSoftware.com) – page flow tests, other view-level tests. Worked fine with ADF. Integrates with JUnit. ( http:// htmlunit.sourceforge.net/gettingStarted.html ) </li></ul></ul><ul><ul><li>HttpUnit (not the same as HtmlUnit) – has a few issues with ADF, apparently. May or may not have problems “playing well with Cactus”. </li></ul></ul><ul><ul><li>Diasparsoft Toolkit – creates fake Servlet requests, and other things. </li></ul></ul><ul><ul><li>Selenium IDE ( http://www.openqa.org/selenium-ide / ) – creates test classes during page navigation. </li></ul></ul><ul><ul><li>Mauve?? – a free suite of functional, blackbox test for the core Java™ libraries. [11] Actually I am not sure about this one </li></ul></ul>
  19. 19. End-to-end testing <ul><li>Recreated Julien Dubois’s scenarios [5]. </li></ul><ul><ul><li>Put his examples together fairly rapidly </li></ul></ul><ul><ul><li>Uses JUnit with Cactus and StrutsTestCase </li></ul></ul><ul><ul><li>Does the same test with JUnit and HttpUnit (Note: for HttpUnit, make sure to manually start your embedded OC4J since you are not doing a “Run” on a web page, actually running your app.) </li></ul></ul>
  20. 20. End-to-end testing <ul><li>Challenges: </li></ul><ul><ul><li>HttpUnit -- had a problem clicking ADF Faces link/button implementations, like: <a href=&quot;#&quot; id=&quot;myform:theSubmitButton&quot; onclick=&quot;submitForm('myform',1,{source:'myform:theSubmitButton'});return false;&quot;> </li></ul></ul><ul><ul><li>this causes an conversion error in the call to a org.mozilla.javascript function. </li></ul></ul>
  21. 21. Demo 3 <ul><li>Some End-to-end testing </li></ul><ul><ul><li>With HttpUnit/HtmlUnit – </li></ul></ul><ul><ul><li>With Cactus – </li></ul></ul><ul><ul><li>Compare approaches </li></ul></ul><ul><ul><ul><li>HttpUnit/HtmlUnit pretends to be a browser, so (as such) it can generate requests and receive responses. </li></ul></ul></ul><ul><ul><ul><li>Cactus actually runs JUnit tests inside the OC4J. You set up one or two Cactus servlets as part of Cactus setup. </li></ul></ul></ul>
  22. 22. Demo 4 <ul><li>Some other test products that work with JDeveloper. </li></ul><ul><ul><li>Fit – simplest example: arithmetic; lots of potential (that I don’t quite have a grip on yet) </li></ul></ul><ul><ul><li>XMLUnit – can tell you… </li></ul></ul><ul><ul><ul><li>XML file diff </li></ul></ul></ul><ul><ul><ul><li>XML validation </li></ul></ul></ul><ul><ul><ul><li>Shows projected outcome of XSLT transformation or an XPath evaluation. </li></ul></ul></ul><ul><ul><li>Selenium IDE – </li></ul></ul><ul><ul><ul><li>extension to Firefox; file download can be run using firefox exe; ADF Faces will run using firefox if firefox is your default browser. </li></ul></ul></ul><ul><ul><ul><li>Can record and play-back keystrokes. </li></ul></ul></ul><ul><ul><ul><li>Very easy to use. </li></ul></ul></ul><ul><ul><ul><li>Also has a browser-based test-runner. </li></ul></ul></ul>
  23. 23. Other kinds of testing <ul><li>Performance/Load/Stress/Scalability/Speed testing [6] </li></ul><ul><ul><li>JMeter </li></ul></ul><ul><ul><li>The Grinder 3 – workers, agents, console…repeat tests just like many people were doing the test over and over from many different machines. Ported to many computers. </li></ul></ul><ul><ul><li>Load test tools on Mercury? </li></ul></ul><ul><ul><li>For EJB apps: Bean-test, EJBQuickTest[7] </li></ul></ul><ul><ul><li>JStyle – critiques Java code style (including some code metrics) [7] </li></ul></ul><ul><ul><li>JTest – supports a variety of tests[7] </li></ul></ul><ul><ul><li>JProbe – profiler/memory debugger[7] </li></ul></ul><ul><li>DbUnit – </li></ul><ul><ul><li>JUnit Extensions </li></ul></ul><ul><ul><li>Puts database into a known state between tests </li></ul></ul><ul><ul><li>Can Import/Export XML datasets, even very large datasets. </li></ul></ul>
  24. 24. Demo 5 <ul><li>Speed testing </li></ul><ul><ul><li>The Grinder 3 – now allows you to write in Jython. (I assume this is a good thing; Grinder-people seemed excited about it.) </li></ul></ul><ul><ul><li>JMeter – Apache desktop application </li></ul></ul><ul><ul><ul><li>Can be used to test files, Servlets, Perl scripts, Java Objects, Databases, FTP servers, … </li></ul></ul></ul>
  25. 25. Zero-product testing <ul><li>“Horrors! No testing software?” </li></ul><ul><li>Remember: it is possible to just let your users take the software for a spin, and get back to you… </li></ul>
  26. 26. Questions? <ul><li>This presentation will be published in the next week or so at the www.rmoug.org website. </li></ul><ul><li>If you have any later contact me at [email_address] ... </li></ul><ul><li>Or try out the JDev forums on technet.oracle.com…good stuff. </li></ul><ul><ul><li>Helpful people </li></ul></ul><ul><ul><li>Knowledgeable people </li></ul></ul><ul><ul><li>Some Oracle JDeveloper Developers monitor this and other forums. (Steve Muench, Shay Shmeltzer, et al ). </li></ul></ul>
  27. 27. Resources <ul><li>“ Testing J2EE Applications” http://www.javaworld.com/javaworld/jw-08-2004/jw-0830-testing.html?page=1 </li></ul><ul><li>“ CVS, Ant, and Junit with JDeveloper” http://www.oracle.com/technology/products/jdev/101/viewlets/101/agile_development_viewlet_swf.html </li></ul><ul><li>Download JDeveloper extensions: JDev Ant Task, JUnit Integration, JUnit Integration for Business Components, JUnit Integration for JDBC. </li></ul><ul><li>Wikipedia: http://en.wikipedia.org/wiki/JUnit </li></ul><ul><li>“ In-container Testing with JUnit”, Julien Dubois, http://www.oracle.com/technology/pub/articles/server_side_unit_tests.html </li></ul><ul><li>Steve Muench in JDev Technet forum: http://forums.oracle.com/forums/thread.jspa?mesageID=1944932&#1944932 </li></ul><ul><li>Dr. Dobb’s: “A J2EE Testing Primer” – (2001) http://www.ddj.com/dept/architect/184414736?cid=Ambysoft </li></ul><ul><li>Httpunit home: http://httpunit.sourceforge.net/doc/tutorial/index.html </li></ul><ul><li>“ Unit Testing with JUnit” – part of JUnit extension in JDeveloper </li></ul><ul><li>Shay Shmeltzer’s blog – http://blogs.oracle.com/shay/2007/08/09#a495 </li></ul><ul><li>Mauve Project – http://sourceware.org/mauve </li></ul><ul><li>Fit – http://fit.c2.com/ </li></ul><ul><li>Example on htmlunit workin for ADF Faces example: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb =get_topic&f=82&t=001139 </li></ul><ul><li>Selenium IDE – http://www.openqa.org/selenium-ide/download.action </li></ul><ul><li>The Grinder – http://grinder.sourceforge.net/ </li></ul><ul><li>XMLUnit -- http://xmlunit.sourceforge.net/ </li></ul><ul><li>DBUnit – http://dbunit.sourceforge.net/ </li></ul><ul><li>JMeter – http://jakarta.apache.org/jmeter/ </li></ul><ul><li>Unit Testing with Mock Objects – http://www.ibm.com/developerworks/library/j-mocktest.html </li></ul>