Testing Options in Java

Post on 19-May-2015

2.419 views 3 download

Tags:

description

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

Transcript of Testing Options in Java

Testing Options in Oracle ADF

Michael A. Fons

Graebel Companies, Inc.

August 2007

Introduction I have been working as an Oracle developer for 17 years.

My daily work has mostly involved PL/SQL, web technolgies, Forms, Reports, …

Java in my career 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).

Nevertheless, J2EE testing and development methodologies sometimes feel foreign to me.

For this presentation… I have spent the last 3 months (after work) trying to learn all I

can about testing Java programs and ADF applications. I hope my perspective helps many people in this transitional

time for Oracle developers.

Outline Topics

What does Oracle offer to test ADF/J2EE app? Different sorts of products available to help test?

This presentation is a survey of some software products which work (or not) with ADF Faces/JSF/ADF BC

Planned Demos (using JDev 10.1.3.2)

1. Test some model components using JUnit test cases.

2. Run a JUnit test script in using Ant automation.

3. Some “view-” (container-) level (black box/end-to-end) testing

4. Some other test products that work with ADF, for different testing purposes.

Testing an ADF Application Research J2EE testing types

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.)

End-to-end/Functional testing – did you build the right features into your app to satisfy the end users’ need(s)? [1]

Integration (with J2EE container) testing Regression/System Integration testing. Load/Speed testing

JDeveloper testing basics JDeveloper primarily offers and promotes testing

of the model layer using… JUnit – to run test scenarios on model components Ant – to help automate testing runs

Another feature you can use for model level testing includes… ADF has easy read/write of (test) bulk data using XML.

(ViewObjects and Rows have readXML and writeXML methods.)

JUnit basics What is JUnit?

A basic unit test framework for Java (hence, “J”-Unit). Very popular; very widely ported

C#, Python, Fortran, Perl, C++, JavaScript [4]

Many sources referenced XP/XM (eXtreme Programming/Modeling in conjunction with JUnit)

Often the foundation of many other testing programs/paradigms

JUnit basics (continued) Components

TestCase -- it is a class you can extend which contains methods which test for particular business rule being implemented effectively or not.

TestSuite – is a class which runs a set of TestCases. (these cases can be specified explicitly or auto-detected)

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.

Ant basics Head, Thorax, Abdomen, six legs…no…not that kind of ant… Ant is like “make”…only better. (Dependency management, XML-based,

extendable, etc.) Ant is often used in automating compiling programs. In this context of this presentation, it automates batch processing JUnit

Tests. JDeveloper can create (Under GeneralAntBuildfile from Project) an Ant

buildfile. Still needs a lot of work, though, to get it working with JUnit and an ADF app. Best to emulate SRDemo build.xml file.

When your ant build.xml (buildScript) is integrated with JUnit and running correctly for you…really neat log of testing results.

JUnit/Ant Installation Run “Check for updates”, under Help menu.

Will need to login using Technet username and password Download and install anything having to do with Ant or JUnit (include the extensions

downloads catagory). Installing Ant will update your DOS PATH variable (so you will be able to

run Ant in a “batch” file). Type “ant -?” on command line for some help. You can learn more, when you set Ant options (in or out of JDev) to be in

“verbose” or “debug” mode. Perhaps better to kick off Ant jobs from within JDeveloper; much less

complex than maintaining a command file. 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.

JUnit TestCase Class To create a test case, extend JUnit TestCase class Contains many “assert*” methods (and also “fail”).

Examples: assertEquals – must find equality between first parameter and

second or assertion fails. assertTrue – boolean value of parameter must be true or the

assertion fails Test methods’

names must have naming convention “test*” If any assert* calls in this method fail, then the outcome of your

test method is failure as well. If all assert* calls succeed, then the outcome of your test is

success.

JUnit: To Test any Pre-existing Method…1. Select the Java program in Application

Navigator2. Select method to test in Structure Pane.3. Right-click to get context menu.4. Click on “New Method test…”5. Select Create or Add to TestCase Class; fill in

other fields as appropriate and submit your changes.

(Note: this does not work with a constructor “method”; “New Method test…” will not be on the context menu in this case.)

JUnit: Create a TestCase for a Method1. Right-click on the project the method’s class is in.2. Click New…3. Under the General category, select the Test Case item,

and click OK.4. In the wizard,

1. 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.

2. 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.

3. Page 3 (optional), specify a test fixture if needed.

Running a Test Case In JDeveloper you can run this test case

class using the Run… context (right-click) menu command. This opens a JUnit Test Runner Window in

JDeveloper; your TestCase(s) are then run, and the results are shown in the window.

Demo 1 Test some model components using JUnit

test cases (using test-first approach). Create a TestSuite for an application Add a test case for an unimplemented business

rule to one of the TestCase classes. Run the test; watch the failure. Implement the business rule. Run the test again; watch it succeed.

Demo 2 Run a JUnit test script in using Ant automation.

This was adapted from SRDemo. 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.

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.

Command line: ??? seems difficult… Cool output log in output directory.

Testing tools… 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]

StrutsTestCase – tests outside the app server with mock-objects

ServletUnit – simulates a Servlet container; alternative to mock-objects.

XMLUnit – tests different aspects of XML files. Cactus – tests inside Servlet container

…more tools… More tools

HtmlUnit (GargoyleSoftware.com) – page flow tests, other view-level tests. Worked fine with ADF. Integrates with JUnit. (http://htmlunit.sourceforge.net/gettingStarted.html )

HttpUnit (not the same as HtmlUnit) – has a few issues with ADF, apparently. May or may not have problems “playing well with Cactus”.

Diasparsoft Toolkit – creates fake Servlet requests, and other things.

Selenium IDE (http://www.openqa.org/selenium-ide/) – creates test classes during page navigation.

Mauve?? – a free suite of functional, blackbox test for the core Java™ libraries. [11] Actually I am not sure about this one

End-to-end testing Recreated Julien Dubois’s scenarios [5].

Put his examples together fairly rapidly Uses JUnit with Cactus and StrutsTestCase 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.)

End-to-end testing Challenges:

HttpUnit -- had a problem clicking ADF Faces link/button implementations, like: <a href="#" id="myform:theSubmitButton" onclick="submitForm('myform',1,{source:'myform:theSubmitButton'});return false;">

this causes an conversion error in the call to a org.mozilla.javascript function.

Demo 3 Some End-to-end testing

With HttpUnit/HtmlUnit – With Cactus – Compare approaches

HttpUnit/HtmlUnit pretends to be a browser, so (as such) it can generate requests and receive responses.

Cactus actually runs JUnit tests inside the OC4J. You set up one or two Cactus servlets as part of Cactus setup.

Demo 4 Some other test products that work with JDeveloper.

Fit – simplest example: arithmetic; lots of potential (that I don’t quite have a grip on yet)

XMLUnit – can tell you… XML file diff XML validation Shows projected outcome of XSLT transformation or an XPath

evaluation. Selenium IDE –

extension to Firefox; file download can be run using firefox exe; ADF Faces will run using firefox if firefox is your default browser.

Can record and play-back keystrokes. Very easy to use. Also has a browser-based test-runner.

Other kinds of testing Performance/Load/Stress/Scalability/Speed testing [6]

JMeter 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.

Load test tools on Mercury? For EJB apps: Bean-test, EJBQuickTest[7] JStyle – critiques Java code style (including some code metrics) [7] JTest – supports a variety of tests[7] JProbe – profiler/memory debugger[7]

DbUnit – JUnit Extensions Puts database into a known state between tests Can Import/Export XML datasets, even very large datasets.

Demo 5 Speed testing

The Grinder 3 – now allows you to write in Jython. (I assume this is a good thing; Grinder-people seemed excited about it.)

JMeter – Apache desktop application Can be used to test files, Servlets, Perl scripts,

Java Objects, Databases, FTP servers, …

Zero-product testing “Horrors! No testing software?” Remember: it is possible to just let your

users take the software for a spin, and get back to you…

Questions? This presentation will be published in the next week

or so at the www.rmoug.org website. If you have any later contact me at

mfons@graebel.com... Or try out the JDev forums on technet.oracle.com…

good stuff. Helpful people Knowledgeable people Some Oracle JDeveloper Developers monitor this and other

forums. (Steve Muench, Shay Shmeltzer, et al).

Resources1. “Testing J2EE Applications” http://www.javaworld.com/javaworld/jw-08-2004/jw-0830-testing.html?page=12. “CVS, Ant, and Junit with JDeveloper”

http://www.oracle.com/technology/products/jdev/101/viewlets/101/agile_development_viewlet_swf.html3. Download JDeveloper extensions: JDev Ant Task, JUnit Integration, JUnit Integration for Business Components, JUnit

Integration for JDBC.4. Wikipedia: http://en.wikipedia.org/wiki/JUnit 5. “In-container Testing with JUnit”, Julien Dubois,

http://www.oracle.com/technology/pub/articles/server_side_unit_tests.html 6. Steve Muench in JDev Technet forum: http://forums.oracle.com/forums/thread.jspa?mesageID=1944932&#1944932 7. Dr. Dobb’s: “A J2EE Testing Primer” – (2001) http://www.ddj.com/dept/architect/184414736?cid=Ambysoft 8. Httpunit home: http://httpunit.sourceforge.net/doc/tutorial/index.html 9. “Unit Testing with JUnit” – part of JUnit extension in JDeveloper 10.1.3.210. Shay Shmeltzer’s blog – http://blogs.oracle.com/shay/2007/08/09#a495 11. Mauve Project – http://sourceware.org/mauve 12. Fit – http://fit.c2.com/ 13. Example on htmlunit workin for ADF Faces example: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb

=get_topic&f=82&t=001139 14. Selenium IDE – http://www.openqa.org/selenium-ide/download.action15. The Grinder – http://grinder.sourceforge.net/ 16. XMLUnit -- http://xmlunit.sourceforge.net/ 17. DBUnit – http://dbunit.sourceforge.net/ 18. JMeter – http://jakarta.apache.org/jmeter/ 19. Unit Testing with Mock Objects – http://www.ibm.com/developerworks/library/j-mocktest.html