Test Driven Development TDD with Maven, Spring Framework, CXF and JUnit When times get tough, the tough get going…
Summary This presentation will cover a single Maven web module, using the Spring framework to create a CXF based webservice, and testing a remote client with JUnit to verify the CXF contract.
This presentation will cover the following topics Maven 2 CXF Spring Framework Spring Remoting Cargo Tomcat JUnit Some of the tools you will need to create your world-wide presence.
Takeaway Attendees will take away a new insight as to how to create setup a Maven web module, how to add Spring, Cargo and CXF capabilities and how to use these technologies to perform unit testing with a test driven development lifecycle.
About the Author Mick Knutson This should begin the presentation.
About Mick Knutson President of BASE Logic, Inc (http://www.baselogic.com) Blog (http://www.baselogic.com/blog) Won ‘Best in Show’ for GoGrid.com @ Linux World 2008 Manage Multiple Enterprise Deployments @ FedEx with >4MIL message transactions per hour.
Setting up a single module Maven project. Setting Up Maven Setting up a single module Maven project. This should begin the presentation.
Maven Quick Start Download Apache Maven (http://maven.apache.org) Unpack Maven to C:/maven Install Sun JDK to c:/jdks/java (NOT C:/Program Files/java) Create workspace for new project (c:/code/ucc2009) Create blank Webapp archetype (template) mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp Open CMD prompt to c:/code/ucc2009
Convention Over Configuration Java source Directory ./src/main/java/** Resources Directory ./src/main/resources/** Webapp Directory ./src/main/webapp/** Testing Source ./src/test/java/** Testing Resources ./src/test/resources/**
Maven Quick start Single Maven Module directory layout
Project Object Model (pom.xml) Project Build Descriptor Group Id Artifact Id Artifact Type (jar, war, ear, pom) Version Dependency Management Plugin Management Build Properties
Adding Spring dependencies In your pom.xml, add the following Spring dependencies Note <property>: <spring.version>2.5.6</spring.version>
Spring Framework Spring does many things, but when you strip it down to its base parts, Spring is a lightweight dependency injection and aspect-oriented container and framework.
Adding Spring Context ./src/main/webapp/WEB-INF/applicationContext.xml
Adding Spring Context ./src/main/webapp/WEB-INF/applicationContext-services.xml
Adding CXF dependencies In your pom.xml, add the following CXF dependencies Note <property>: <cxf.version>2.2.3</cxf.version>
Adding CXF Context Server entries Create applicationContext-services.xml
Adding web.xml entries Adding Spring and CXF entries
Adding JUnit Test Case Create new Test Case Instantiate Test Spring Context Get Client Proxy Bean Execute Proxy Methods on Bean Assert outcome.
Adding JUnit Test Create ./src/test/java/**/MessageWebServiceTest Talk about the Unit test, and how Spring starts the IoC container.
Create Service for Test Case Create Service to match our test
Create Service for Test Case Create Service Interface Annotate the Interface as @WebService Create Service Implementation Class Annotate the Class as @WebService defining the Service Interface as the endpointInterface.
Adding Service Interface Create src/main/java/**/MessageService Talk about the Unit test, and how Spring starts the IoC container.
Adding Service Implementation Create src/main/java/**/MessageServiceImpl Talk about the Unit test, and how Spring starts the IoC container.
Adding Spring Test Context Create applicationContext-test.xml
Adding test.properties Create src/test/resources/test.properties
Cargo Tomcat Plugin for Maven Compile and Package War Start embedded Tomcat container before running Tests Deploy war to Tomcat container Run Test Cases Assert Test Outcome Stop embedded Tomcat container after tests complete.
Cargo Tomcat Plugin We want to start Tomcat with the Cargo plugin and deploy our webapp before running unit tests
Running Standalone Cargo Plugin By running run-cargo-tomcat.bat, we can deploy our war, and verify our service endpoint in a browser manually Run-cargo-tomcat.bat executes: mvn -Dcargo.wait=true cargo:start -e
CXF Generated WSDL
Tying it all together Compile and Package war Start Cargo Tomcat Deploy war to Tomcat Run Tests Assert Results Stop Cargo Tomcat
Start Cargo, and Run Test Executing mvn clean verify
Test Success Successful Test result
Next Steps Debug-As JUnit Tests in Eclipse Remote Debugging with Eclipse Use Easy Mock to stub Spring services Integrate Coburtura to measure Test Code Coverage
Any questions of comments? This is the end of the presentation…
Thank you for your time and attention… The End… Thank you for your time and attention… This is the end of the presentation…
Mick Knutson BASE Logic, Inc. Enterprise Architecture, Design, Mentoring & Agile Consulting Phone. (866) BLiNC-411: (254-6241-1) Fax. (415) 685-4233 Website: http://www.baselogic.com Website: http://www.baselogic.com/blog Linked IN: http://www.linkedin.com/in/mickknutson
References Source Code (http://baselogic.com/code/ucc2009/webapp.zip) Slide Deck PPT (http://baselogic.com/code/ucc2009/ucc.ppt) Apache Maven (http://maven.apache.org) Springframework (http://www.springsource.com) CXF (http://cxf.apache.org) Cargo Plugin (http://cargo.codehaus.org/Maven2+plugin) SOAP UI (http://soapui.org)