Data-Driven Unit Testing for Java

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    Data-Driven Unit Testing for Java - Presentation Transcript

    1. Advanced Unit Testing for Java By Denilson Nastacio Data-Driven Unit Testing
    2. Your Host
      • Denilson Nastacio
      • 14 years in software development
      • Quality Enthusiast
      • Rational Unified Process practitioner
      • RTP Scrolls ( http://rtpscrolls.blogspot.com )
      • SourcePatch ( http://sourcepatch.blogspot.com )
      • LinkedIn ( http://www.linkedin.com/in/nastacio )
    3. Introduction
      • Example
      • Your guessed right, a bank account :-)
      • Conventional unit testing
      • Adequate…sometimes
      • When coverage is not everything
      • Multiple combinations of input values
      • Multiple internal states
      • Complex behavior
      • The Contenders
      • JUnit 3.8
      • JUnit 4.x
      • TestNG
      • DDTUnit
    4. Checking Account
      • public class Account {
      • private float balance;
      • private float lineOfCredit;
      • public Account( float balance, float lineOfCredit) {
      • super ();
      • this .balance = balance;
      • this .lineOfCredit = lineOfCredit;
      • }
      • public void deposit( float value) {
      • if (value < = 0) {
      • throw new IllegalArgumentException( “ Deposit value (“ + value + “) is not grater than 0 &quot; );
      • }
      • balance += value;
      • }
      • public float getBalance() {
      • return balance;
      • }
    5. Junit 3.8
      • public void test Valid Deposit() {
      • Account acc = new Account(0f , 0f);
      • acc.deposit(50f);
      • assertEquals (50f, acc.getBalance(), 0f);
      • }
      • public void test Zero Deposit() {
      • Account acc = new Account(0f , 0f);
      • try {
      • acc.deposit(0f);
      • fail( &quot;Deposit should fail&quot; );
      • } catch (IllegalArgumentException e) {
      • // expected
      • }
      • }
      • public void test Negative Deposit() {
      • Account acc = new Account(0f , 0f);
      • try {
      • acc.deposit(-50f);
      • fail( &quot;Deposit should fail&quot; );
      • } catch (IllegalArgumentException e) {
      • // expected
      • }
      • }
    6. JUnit 3.8 – Pros and Cons
      • Pros
      • Short learning curve
      • Trivial to write
      • Cons
      • Tedious to write
      • Long files
      • Input data mixed with source code: difficult to glean all input combinations
    7. JUnit 4.x – Using parameterized tests
      • @RunWith (Parameterized. class )
      • public class AccountTest extends TestCase {
      • private float lineOfCredit;
      • private float deposit;
      • private Object result;
      • @Parameters
      • public static Collection data() {
      • return Arrays. asList ( new Object[][] {
      • { 0f, 50f, 50f },
      • { 0f, 0f, new IllegalArgumentException() },
      • { 0f, -5 0f, new IllegalArgumentException() } });
      • }
      • public AccountTest( float lineOfCredit, float deposit,
      • Object result) {
      • this .lineOfCredit = lineOfCredit;
      • this .deposit = deposit;
      • this .result = result;
      • }
      • @Test
      • public void deposit() {
      • Account acc = new Account(0.0f, lineOfCredit);
      • boolean expectError = false ;
      • if (result instanceof IllegalArgumentException) {
      • expectError = true ;
      • }
      • try {
      • acc.deposit(deposit);
      • if (expectError) {
      • fail( &quot;Deposit should fail&quot; );
      • } else {
      • assertEquals(acc.getBalance(),
      • ((Float) result).floatValue(), 0f);
      • }
      • } catch (IllegalArgumentException e) {
      • if (!expectError) {
      • throw e;
      • }
      • }
      • }
    8. JUnit 4.x – Pros and Cons
      • Pros
      • Built-in separation of data and logic
      • Good integration with Eclipse
      • Cons
      • One class per collection of input parameters
      • Difficult to handle collections of input parameters for verification of successful runs versus exception handling.
      • No labels for parameter sets
    9. TestNG
      • <!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot;>
      • <suite name=&quot;banking-test&quot;>
      • <test name=&quot;testValidDeposit&quot;>
      • <parameter name=&quot;lineOfCredit&quot; value=&quot;0f&quot;/>
      • <parameter name=&quot;deposit&quot; value=&quot;50f&quot;/>
      • <parameter name=&quot;result&quot; value=&quot;50f&quot;/>
      • <classes>
      • <class name=&quot; com.ibm.qse.banking.AccountTestNgTest &quot;>
      • <methods>
      • <include name=&quot; testDeposit &quot;/>
      • </methods>
      • </class>
      • </classes>
      • </test>
      • <test name=&quot;testZeroDeposit&quot;>
      • <parameter name=&quot;lineOfCredit&quot; value=&quot;0f&quot;/>
      • <parameter name=&quot;deposit&quot; value=&quot;0f&quot;/>
      • <parameter name=&quot;result&quot; value=&quot;IllegalArgumentException&quot;/>
      • <classes>
      • <class name=&quot; com.ibm.qse.banking.AccountTestNgTest &quot;>
      • ...
      • </class>
      • </classes>
      • </test>
      • <test name=&quot;testNegativeDeposit&quot;>
      • <parameter name=&quot;lineOfCredit&quot; value=&quot;0f&quot;/>
      • <parameter name=&quot;deposit&quot; value=&quot;-50f&quot;/>
      • <parameter name=&quot;result&quot; value=&quot;IllegalArgumentException&quot;/>
      • <classes>
      • <class name=&quot; com.ibm.qse.banking.AccountTestNgTest &quot;>
      • ...
      • </class>
      • </classes>
      • </test>
      • </suite>
      Selected methods must be specified in each “test”element
    10. TestNG – Parameterized tests – Java source
      • public class AccountTestNgTest {
      • @Test
      • @Parameters( { &quot;lineOfCredit&quot;, &quot;deposit&quot;, &quot;result&quot; })
      • public void testDeposit( float lineOfCredit, float deposit, String result) {
      • Account acc = new Account(0f, lineOfCredit);
      • float expectedBalance = 0f;
      • try {
      • expectedBalance = Float. parseFloat (result);
      • acc.deposit(deposit);
      • assertEquals (expectedBalance, acc.getBalance());
      • } catch (NumberFormatException e) {
      • try {
      • acc.deposit(deposit);
      • fail ( &quot;Deposit should fail&quot; );
      • } catch (IllegalArgumentException e1) {
      • // expected
      • }
      • }
      • }
      TestNG does not support complex Java objects
    11. TestNG – Pros & Cons
      • Pros
      • Complete separation of data and logic; separate files
      • Allows named parameter sets
      • Good support for grouping tests
      • Good integration with Eclipse
      • Cons
      • No support for complex Java types in the testng.xml file (that is a big one)
      • Tests in the xml file grouped by parameters instead of class-methods, must map each input sets to a method every time
      • JUnit competitor (increased learning curve)
      • Additional Eclipse plug-in required
    12. DDTUnit – XML file (DDT-AccountTest.xml)
      • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
      • <ddtunit xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      • xsi:noNamespaceSchemaLocation=&quot;ddtunit.xsd&quot;>
      • <cluster id=&quot; AccountTest &quot;>
      • <group id=&quot; testDeposit &quot;>
      • <test id=&quot;valid&quot;>
      • <objs>
      • <obj id= &quot;lineOfCredit&quot; type=&quot;float&quot;>50</obj>
      • <obj id= &quot;deposit&quot; type=&quot;float&quot;>50</obj>
      • </objs>
      • <asserts>
      • <assert id= &quot;balance&quot; type=&quot;float&quot; action=&quot;isEqual&quot;>50.0f</assert>
      • </asserts>
      • </test>
      • <test id=&quot;zero&quot;>
      • <objs>
      • <obj id=&quot;lineOfCredit&quot; type=&quot;float&quot;>50</obj>
      • <obj id=&quot;deposit&quot; type=&quot;float&quot;>0</obj>
      • </objs>
      • <asserts>
      • <exception id=&quot;expectedException&quot; type=&quot;java.lang.IllegalArgumentException&quot;
      • action=&quot;isSimilar&quot;>0</exception>
      • </asserts>
      • </test>
      • <test id=&quot;negative&quot;>
      • <objs>
      • <obj id=&quot;lineOfCredit&quot; type=&quot;float&quot;>50</obj>
      • <obj id=&quot;deposit&quot; type=&quot;float&quot;>-50</obj>
      • </objs>
      • <asserts>
      • <exception id=&quot;expectedException“ type=&quot;java.lang.IllegalArgumentException&quot;
      • action=&quot;isSimilar&quot;>-50</exception>
      • </asserts>
      • </test>
      • </group>
      • </cluster>
      • </ddtunit>
      Class name Method name
    13. DDTUnit – Java source
      • public class AccountDDTUnitTest extends DDTTestCase {
      • public void testDeposit() {
      • float lineOfCredit = ((Float)getObject( &quot;lineOfCredit&quot; )).floatValue();
      • float deposit = ((Float)getObject( &quot;deposit&quot; )).floatValue();
      • Account acc = new Account(0f,lineOfCredit);
      • acc.deposit(deposit);
      • addObjectToAssert( &quot;balance&quot; , new Float(acc.getBalance()));
      • }
      • protected void initContext() {
      • initTestData( &quot;AccountTest&quot; );
      • }
      • }
      Inherits from junit.framework.TestCase DDTUnit definition file: DDT-AccountTest.xml
    14. DDTUnit – Pros & Cons
      • Pros
      • Complete separation of data and logic; separate files
      • Allows the cleanest Java source code
      • Support for complex Java types
      • Support for verification of exceptions
      • Supports global test data
      • Based on JUnit
      • Good support for grouping tests
      • Cons
      • XML syntax can be a language on its own when dealing with complex structures
      • Poor integration with Eclipse, programmer must rely on console outputs as well
    15. DDTUnit – A closer look http://www.flickr.com/photos/xfrf/2260850105/ by xfrf
    16. DDTUnit Example: Complex object with a string constructor
      • Class constructor
      • public ObjectName(String name) { ... }
      • Inside DDT-<namekey>.xml file
      • <obj id=&quot; jmxId &quot; type=&quot;javax.management.ObjectName&quot;>
      • <item>WebSphere:type=cellManager</item>
      • </obj>
      • Inside TestCase java file
      • javax.management.ObjectName jmxId =
      • (javax.management.ObjectName)getObject(&quot; jmxId &quot; );
    17. DDTUnit Example: Complex object with a complex constructor
      • Class constructor
      • public Attribute(String name, String value) {... }
      • Inside DDT-<namekey>.xml file
      • < obj id= &quot; jmxAttr &quot; type=&quot;javax.management.Attribute&quot; hint=&quot;call&quot;
      • method=&quot;constructor&quot;>
      • <item type=&quot;string&quot;>fileName</item>
      • <item type=&quot;string&quot;> directory< /item>
      • </item>
      • Inside TestCase java file
      • javax.management. Attribute jmxId =
      • (javax.management. Attribute )getObject(&quot; jmxAttr &quot; );
    18. DDTUnit Example Defining a java.util.ArrayList
      • Inside DDT-<namekey>.xml file
      • <obj id= “ profileLocations &quot; type=&quot;java.util. ArrayList &quot; hint=&quot;collection&quot; valuetype=&quot;string&quot;>
      • <item>/opt/IBM/WebSphere/AppServer/profiles/s1</item>
      • <item>/opt/IBM/WebSphere/AppServer/profiles/s 2 </item>
      • <item>/opt/IBM/WebSphere/AppServer/profiles/s 3 </item>
      • </obj>
      • Inside TestCase java file
      • ArrayList <String> profileLocations =
      • ( ArrayList <String>)getObject(&quot; profileLocations &quot; );
    19. DDTUnit Example Array of complex objects
      • Inside DDT-<namekey>.xml file
      • <obj id=&quot; arrayId &quot; type=“ java.util .A rray List&quot; hint=&quot;collection&quot;>
      • <item type=&quot;javax.management.Attribute&quot; hint=&quot;call&quot; method=&quot;constructor&quot;>
      • <item type=&quot;string&quot;>fileName</item>
      • <item type=&quot;string&quot;>${SERVER_ROOT}/trace.log</item>
      • </item>
      • </obj>
      • Inside TestCase java file
      • ArrayList<Attribute > profileLocations =
      • ( ArrayList < Attribute >)getObject(&quot; arrayId &quot; );
    20. DDTUnit and Eclipse Snippets http://sourcepatch.blogspot.com/2009/08/ddtunit-and-eclipse-snippets.html
    21. Other noteworthy frameworks
      • JTestCase
        • Similar to DDTUnit but lacks a test runner
        • Requires testcase to extract data from XML file and pass it to fixtures
      • Fit
        • High-level data definition using MS-Word and MS-Excel
        • Inadequate for unit testing, support for strings and basic numeric types
      • Jetif
        • Full replacement for JUnit + JTestCase
        • Lost me on “full replacement”
    22. Resources
      • JUnit ( http:// www.junit.org / )
      • DDTUnit ( http://sourceforge.net/projects/ddtunit )
      • TestNG ( http://testng.org/doc/ )
      • JTestCase ( http://jtestcase.sourceforge.net/ )
      • Jetif ( http:// jetif.sourceforge.net )
      • FIT ( http://fit.c2.com/ )
      • My del.icio.us bookmarks for this presentation (tag: qse-ddt)
        • http://delicious.com/nastacio/qse-ddt

    + Denilson NastacioDenilson Nastacio, 3 months ago

    custom

    356 views, 2 favs, 2 embeds more stats

    An overview of the mainstream data-driven test fram more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 356
      • 351 on SlideShare
      • 5 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 0
    Most viewed embeds
    • 3 views on https://www.calgbapps.org
    • 2 views on http://sourcepatch.blogspot.com

    more

    All embeds
    • 3 views on https://www.calgbapps.org
    • 2 views on http://sourcepatch.blogspot.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories