Test-driven development of business logic that uses WebSphere business objects

Learn how to set up a Java™ SE development environment to facilitate test-driven development of SOA business logic that uses WebSphere® business objects. This environment lets you run JUnit test cases outside of WebSphere Process Server.

Share:

Albert Chung (alchung@us.ibm.com), Advisory Software Engineer, IBM

Albert Chung photoAlbert Chung is a Software Engineer at the IBM Research Triangle Park Lab in Durham, North Carolina. He is currently the lead developer on the WebSphere Business Object Framework team.



13 July 2011

Also available in Chinese

Introduction

Test-driven development (TDD) is a key practice in agile software development. TDD is a style of coding where the desired behavior or specification is written first in the form of an executable example (typically as a JUnit test case). TDD is an iterative process and requires you to run the test cases after small incremental changes to the code during code development. Therefore, for TDD to be effective, you must run such quickly.

Currently, if you want to test your business logic that uses WebSphere business objects, you must deploy your code to a running server in order to run it. This makes TDD impractical. Wouldn’t it be simpler if you can develop your code and run your JUnit test cases in a simple Java SE environment without having to deploy the test cases to the server? This article shows you how to set up a simple Java SE development environment on any Eclipse-based integrated development environment (IDE). This environment allows you to work with business objects (create, manipulate, copy, compare, serialize, and so on). It then shows you how to use this development environment to implement a simple service-oriented architecture (SOA) business logic using the TDD way.

Sample project file

In the Download section, you will find the SampleProject.zip file. This file is the Java project that contains the completed source code, JUnit test case, and schema used in this article.


Set up the Eclipse development environment

You can use any Eclipse-based IDE. For example, you can use WebSphere Integration Developer or simply download the base Eclipse-IDE from the Eclipse.org site.

Follow these steps to set up the development environment:

  1. Start the Eclipse IDE and create a new workspace.
  2. Switch to the Java perspective.
  3. Select File > New > Java Project.
  4. Enter BOJUnit as the Java Project name.
  5. Click the Configure JREs... link to open the preference dialog for Installed JREs as shown in Figure 1.
    Figure 1. Installed JREs dialog
    Installed JREs dialog
  6. Click the Add button to open the Add JRE dialog (see Figure 2).
  7. Select Standard VM as the JRE type, then click Next.
    Figure 2. Add JRE dialog
    Add JRE dialog
  8. Click the Directory... button and browse to your WebSphere Process Server JRE directory. For example, select the C:\W75\java directory if C:\W75 is your WebSphere Process Server (hereafter called Process Server) install location.
  9. Enter WPS_JRE as the JRE name and click Finish.
  10. Click OK to exit the preference dialog and return to the New Java Project dialog.
  11. Select the Use a project specific JRE radio button, and then select WPS_JRE you just defined.
  12. Click Next on the Java Settings page.
  13. Select the Libraries tab and the Add Library... button to open the Add Library dialog.
  14. Select JUnit > Next to open the JUnit Library dialog.
  15. Select JUnit4 as the JUnit version from the drop down list and click Finish.
  16. Back on the Java Settings page, select the Add Library... button to open the Add Library dialog. This time select User Library > Next to open the User Library dialog.
  17. Click User Libraries... > New... and enter WPS as the user library name.
  18. Back on the User Library page, select WPS, then select the Add Jars... button.
  19. Select C:\W75\lib\bootstrap.jar (substitute C:\W75 with your Process Server install location).
  20. Back on the User Library page, select WPS and then select the Add Jars... button again.
  21. This time, select all the JAR files in the C:\W75\plugins directory (substitute C:\W75 with your Process Server install location) as shown in Figure 3.
    Figure 3. WPS User Libraries
    WPS User Libraries
  22. Click OK > Finish.
  23. Back on Java Settings page, click the Order and Export tab as shown in Figure 4 and move WPS JRE to the bottom of the list.
    Figure 4. Order and Export tab (for version 6.x)
    Order and Export tab (for version 6.x)
  24. Click Finish.

For Process Server version 6.x, your development environment setup is complete.

For Process Server version 7 or higher, perform these additional steps:

  1. On the Java Settings page, select the Libraries tab.
  2. Click User Libraries... > New... and enter FeaturePack as the user library name.
  3. On the User Library page, select FeaturePack, then select the Add Jars... button.
  4. Add these JARs s to the FeaturePack user library (substitute C:\W75 with your Process Server install location) as shown in Figure 5:
    1. C:\W75\feature_packs\sca\sdoplugins\sdo_api.jar
    2. C:\W75\feature_packs\sca\sdoplugins\sdoe.jar
    3. C:\W75\feature_packs\sca\sdoplugins\sdox_api.jar
    4. C:\W75\feature_packs\xml\plugins\com.ibm.xml.jar
    Figure 5. FeaturePack User Libraries
    FeaturePack User Libraries
  5. Click OK > Finish to return to the Java Settings page.
  6. Click the Order and Export tab and move the FeaturePack user library to the top of the list as shown in Figure 6.
    Figure 6. Order and Export tab (for version 7.x)
    FeaturePack User Libraries
  7. Click Finish.

This completes the setup of your development environment. With this environment, you can test business objects as if they are running in a Process Server environment. For example, you can create BOs, copy BOs, serialize BOs, and so on in this Java SE development environment. The following section shows an example of creating a customer BO, and then setting the name to this BO in the development environment.


Implement the business logic using the TDD way

This section uses a simple scenario to show how you can practice TDD using the development environment created in the previous section.

Scenario requirement

Develop a behavior in your SOA application’s business logic that can fully populate a customer business object with data. To make the scenario simple, you will only populate a customer BO with the first name and last name.

TDD work flow

This article assumes you are already familiar with TDD. The focus in this section is to show how you can practice TDD while developing business logic that uses business objects. The illustration in Figure 7 shows the steps to follow while using TDD as your coding style.

Figure 7. TDD style of the software development work flow
TDD style of the software development work flow

Writing the test case

The first step in TDD is to describe the required behavior in the form of an executable example. In other words, use a JUnit test case to capture the scenario requirement.

Follow these steps to create the JUnit test case:

  1. In the BOJunit Java project you created earlier, add a new Java class named CustomerTest.java. Click on File > New > Class.
  2. Copy and paste the code shown in Listing 1 into CustomerTest.java:
    Listing 1. JUnit test class CustomerTest.java
    importstatic org.junit.Assert.assertEquals;
    import org.junit.Test;
    import com.ibm.websphere.bo.BOFactory;
    import com.ibm.websphere.sca.ServiceManager;
    import commonj.sdo.DataObject;
    
    publicclass CustomerTest {
    
     @Test
     publicvoid populateCustomer() {
      BOFactory boFactory = (BOFactory) ServiceManager.INSTANCE.locateService
       ("com/ibm/websphere/bo/BOFactory");
      DataObject dobj = boFactory.create("http://www.ibm.com/Customer","Customer");
      MyBusinessLogic myLogic = new MyBusinessLogic();
      myLogic.populateCustomer(dobj, "John", "Doe");
      assertEquals("John", dobj.get("firstName"));
      assertEquals("Doe", dobj.get("lastName"));
    	}
    }
  3. Notice that there is compilation error as MyBusinessLogic class does not exist yet. Click the line MyBusinessLogic, and then press Ctrl + 1. On the context menu, select Create class “MyBusinessLogic” to have Eclipse automatically generate this class for you.
  4. Go back to the Java editor for CustomerTest.java. Now the compilation error occurs on myLogic.populateCustomer. Click this line, then press Ctrl + 1. On the context menu, select the Create method myLogic.populateCustomer(DataObject, String, String) in the type MyBusinessLogic to have Eclipse automatically generate this method for you.
  5. There should be no more compilation errors in the Java project. Now you can proceed to Step 2 of the TDD style of the software development work flow, which is to run the test case to failure. Right-click the Java editor for CustomerTest.java to open the context menu.
  6. Click Run as > JUnit test.
  7. As expected, the test failed with the following error message: java.lang.NullPointerException. This is because you have not yet defined the Customer in a schema, which caused the boFacory.create method to return a null. Create the Customer.xsd file as shown in Listing 2, then copy and paste it to the src directory (you can put schemas and WSDLs in any directory, as long as it is in the classpath).
    Listing 2. Customer.xsd schema
    <?xml version="1.0" encoding="UTF-8"?>
    <schema targetNamespace="http://www.ibm.com/Customer" 
    	xmlns="http://www.w3.org/2001/XMLSchema">
    	<complexType name="Customer">
    	 <sequence>
    		<element name="firstName" type="string" />
    		<element name="lastName" type="string" />
    	 </sequence>
        </complexType>
    </schema>
  8. Repeat Steps 5 and 6 to rerun the JUnit test.
  9. This time, the test failed with the following error message: java.lang.AssertionError: expected:<John> but was:<null>. This is expected as you have not implemented the business logic in the MyBusinessLogic.populateCustomer method. Implement this method with the code shown in Listing 3.
    Listing 3. MyBusinessLogic.java
    import commonj.sdo.DataObject;
    
    publicclass MyBusinessLogic {
     publicvoid populateCustomer(DataObject dobj, String firstName, 
      String lastName) {
    	dobj.set("firstName", firstName);
    	dobj.set("lastName", lastName);
      }
    }
  10. Repeat Steps 5 and 6 to rerun the JUnit test. This time, the test case passed. This is Step 4 of the TDD style of the software development work flow. Given that you are just writing one test case and the business logic implementation is trivial in the example, there is no need to do Steps 5, 6, and 7 of the TDD style of the software development work flow.

As you can see from the steps described above, TDD is an iterative process and you can rely on running the JUnit test case after incremental code changes. The test result not only tells you whether the test case passed or failed, but also tells you what the problem is. This allows you to fix the problem one at a time (run test > failure > fix code > run test > failure > fix code > fix code > run test > success).


Conclusion

This article demonstrated how to set up a Java SE development environment to facilitate TDD of an SOA business logic that uses WebSphere business objects. This environment allows you to run JUnits test cases outside of the WebSphere Process Server environment. It also used a simple scenario to illustrate a TDD style of development. The end result is an implementation of the business logic required and a JUnit test case that can verify the behavior of the implementation.


Download

DescriptionNameSize
Sample codeSampleProject.zip4KB

Resources

Learn

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Business process management on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Business process management, WebSphere, Agile transformation
ArticleID=732108
ArticleTitle=Test-driven development of business logic that uses WebSphere business objects
publish-date=07132011