Your SlideShare is downloading. ×
  • Like
Unit Testing RPG with JUnit
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Unit Testing RPG with JUnit



Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Unit Testing RPG with JUnit"Never in the field of softwaredevelopment was so much owed byso many to so few lines of code"Martin Fowler (speaking of JUnit)
  • 2. Agenda Costs of Manual Testing Value of Automated Unit Testing The Tools We’ll Use Creating the Infrastructure Reviewing the Component to be Tested The Test Components Writing the interface code and the Tests Compiling the Java Running the Tests Automating the Process
  • 3. Costs of Manual Testing Manual testing generally occurs late, justbefore integration. The cost to correctproblems at this stage are ten to a hundredtimes greater than the cost to correct theproblem before the project reaches thisstage. A problem found late in the developmentcycle can delay the carefully planned workof a dozen people.
  • 4. Value of Automated Testing Writing tests before writing the applicationcode increases cohesion and reducescoupling. You won’t understand it until you’ve doneit. The tests are reusable. Reuse is free.Write tests once; verify quality a thousandtimes without lifting a finger.
  • 5. The Tools We’ll Use The JUnit JAR file (junit.jar) iSeries QSHELL and shell scripts javac, the Java compiler JNI, Java Native Interface iSeries Navigator iSeries IFS (integrated file system) PC-based Text Editor iSeries Source Editor RPG Compiler
  • 6. Creating the Infrastructure Add a “Share” to your IFS folder. Map the share to Windows Explorer. Place a copy of the JUnit jar file in your IFSfolder.
  • 7. To add the file share, use iSeries Navigatorand select the Integrated File System on theiSeries on which you’ll be developing.
  • 8. Find or create your IFS folder. Werequire one to be under Root/home.
  • 9. Right click and select Sharing/NewShare…
  • 10. Create the file system shareBe sure to select Read/Write access!
  • 11. In Windows Explorer, select Tools/MapNetwork Drive…
  • 12. Complete mapping a drive to the sharecreated earlier.Example: servershare
  • 13. Getting the JAR File Download JUnit from Open the Zip file, select the junit.jar andextract it your development folder on theiSeries share drive.
  • 14. Next Steps Review the ILE RPG procedures we willtest. Create the test components. Run the tests.
  • 15. Review the RPG Procedures to be Tested* Trivial Example: Add and Subtract*h nomainh option(*srcstmt:*nodebugio)h datfmt(*ISO)*d addInts pr 15P 0 extproc(addInts)d 15P 0 CONSTd 15P 0 CONST*d subInts pr 15P 0 extproc(subInts)d 15P 0 CONSTd 15P 0 CONST*p addInts b EXPORTd pi 15P 0d p1 15P 0 CONSTd p2 15P 0 CONSTc return p1 + p2p e*p subInts b EXPORTd pi 15P 0d p1 15P 0 CONSTd p2 15P 0 CONSTc return p1 - p2p e
  • 16. The Test ComponentsTo create and run the tests, 4 objectsare required: The service program to be tested. The service program that provides thenative interface to Java. The Java Class that accesses the RPGservice program. The class that implements the unit test.
  • 17. Parameter Conversion TableThe necessary information for converting parameter values isin chapter 11 of the RPG Programmer’s Guide.
  • 18. A second RPG module is required1. the *JAVA keyword2. the fully qualified name of the Java classthat will execute this procedure.3. the name of the Java method (markednative) which will call the ILE procedure.For Java to access the RPG, the ILEprocedures must be given a Java interfacewith the EXTPROC keyword.When interfacing with Java, the EXTPROCkeyword requires three parameters:
  • 19. h nomainh option(*srcstmt:*nodebugio)h datfmt(*ISO)* Imported Proceduresd addInts pr 15P 0 extproc(addInts)d 15P 0 CONSTd 15P 0 CONST*d subInts pr 15P 0 extproc(subInts)d 15P 0 CONSTd 15P 0 CONST* Exported Proceduresd add pr 10I 0 extproc(*JAVA:d com.rpg.Math‘ : add)d 10I 0 valued 10I 0 value*d sub pr 10I 0 extproc(*JAVA:d com.rpg.Math‘ : sub)d 10I 0 valued 10I 0 value*p add b EXPORTd pi 10I 0d p1 10I 0 valued p2 10I 0 valuec return addInts(p1: p2)p e*p sub b EXPORTd pi 10I 0d p1 10I 0 valued p2 10I 0 valuec return subInts(p1: p2)p e
  • 20. Compiling the RPGCreate the RPG modules:crtrpgmod calculatorcrtrpgmod calcjniCreate the service program with or withoutbinding source:crtsrvpgm jniformath module(calcjnicalculator) export(*all)
  • 21. Write the Java code that calls RPGpackage com.rpg;public class Math {static {System.loadLibrary("JNIFORMATH");}native public int add(int add1, int add2);native public int sub(int sub1, int sub2);}The JNI magic necessary for Java to call RPGsimply requires (1.) the name of the serviceprogram to be provided in the parameter of thecall to System.loadLibrary() and (2.) thecreation of the native method signatures.Your PC’s NotePad or WordPad editor is sufficient for this task.
  • 22. The Unit Testimport junit.framework.*;public class MathTest extends TestCase {Math math;protected void setUp() {math = new Math();}public void testAdd() {Assert.assertEquals(2, math.add(2,0));Assert.assertEquals(2, math.add(1,1));}public void testSub() {Assert.assertEquals(0, math.sub(2,2));Assert.assertEquals(0, math.sub(1,1));}}This class inherits from junit.framework.TestCase. To compileand run, junit.jar must be in the classpath.
  • 23. Save the Java source to the iSeries’ IFS usingthe drive we mapped earlier, being careful tomatch folders to the package statements.
  • 24. Interaction of the Software Components
  • 25. Preparing to Compile the Java SourceThe Java source is in folders in theiSeries’s IFS. We will compile and runthe Java programs in the QSHELLenvironment on the iSeries.From the command line of the iSeriessystem where the Java and RPG areplaced, enter the command QSH tostart the alternative Unix shellenvironment on iSeries.
  • 26. Commands Useful in QSHELL LS – list files (similar to MS-DOSDIR command) PWD – display name of currentfolder CD – change directory (just as inMS-DOS) CAT – takes a file name as itsparameter and displays file’s text
  • 27. Java’s Compile Command JAVAC is the Java compiler. It canbe run by an IDE or from thecommand line. It is most convenientin this exercise to run JAVAC fromQSHELL.
  • 28. Compile Dependencies The classes you compile in Javamay be dependent on JAR files justas the compile of RPG programs canbe dependent on bindingdirectories, service programs andmodules. The Test classes will depend onjunit.jar.
  • 29. Compiling With JAVACThe Math class provides the JNI interface toRPG. It was placed in the com.rpg package sothe compile command is as follows:javac com/rpg/Math.javaThe MathTest class inherits fromjunit.framework.TestCase so, the junit.jar filemust be in the classpath when it is compiled:javac –classpath .:../lib/
  • 30. Running With JAVATo execute Java Classes, the JAVA commandis used instead of CALL. Note that theclasspath contains the junit.jar file.Also note that we are calling junit’sTestRunner class and passing it the name ofour test -classpath .:../lib/junit.jarjunit.textui.TestRunner MathTestThe entire JAVA command above wraps to two lines but it is notrequired that it do so.
  • 31. Compile and Run the Java
  • 32. Automating the Process: Run the Compileand Test in a Shell Scriptif !(javac -classpath .:../lib/junit.jarsrc/** -d build/prod); then exit 1; fi;jar -cf Math.jar build/prod/*.classjava -classpath .:../lib/junit.jar:Math.jarjunit.textui.TestRunner MathTestANT is another neat tool from the Open Sourcecommunity. ANT could be used to automate these tasksbut, that requires quite a long discussion.A QSHELL script also provides a means to easily compilethe Java and run the tests. Create the script by placingcommands similar to those below in a file.
  • 33. Results of Running the ScriptA compile error interrupts the script when it is firstrun. After the error in the Java source is corrected,the script runs successfully, performing both thecompile and the testing.
  • 34. Next Steps Don’t stop with just these tests.Create more. Find out what breaksyour application’s code and attackthose problems. Implement a way for your team toshare, store and version tests. Learn Test Driven Development.
  • 35. References1. JUnit: http://junit.org2. QSHELL for iSeries by Ted Holt and FredA. Kulack: http://skillport.books24x7.com3. JNI Articles: JNI RPG and the eBusiness World (ch 11)
  • 36. References (continued) Sun’s Tutorial – First Steps for Unix: