Test Driven Development

namibiancurrishInternet and Web Development

Nov 12, 2013 (3 years and 9 months ago)

74 views

Test Driven Development

TDD with Maven, Spring Framework,
CXF and JUnit

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

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

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 Maven

Setting up a single module Maven
project.

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

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

Adding Service Implementation

Create
src/main/java/**/MessageServiceImpl

Adding Spring Test Context

Create applicationContext
-
test.xml

Adding test.properties

Create src/test/resources/test.properties

Cargo Tomcat Plugin for Maven

1.
Compile and Package War

2.
Start embedded Tomcat container before
running Tests

3.
Deploy war to Tomcat container

4.
Run Test Cases

5.
Assert Test Outcome

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

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

Questions?

Any questions of comments?

The End…

Thank you for your time and
attention…

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
)