• No results found

Maven 2 build system overview General build procedure Multi-module project build process Spring IoC integration Testing with Maven 2 and Spring

N/A
N/A
Protected

Academic year: 2019

Share "Maven 2 build system overview General build procedure Multi-module project build process Spring IoC integration Testing with Maven 2 and Spring"

Copied!
44
0
0

Loading.... (view fulltext now)

Full text

(1)

© 2008 coreservlets.com

Spring Integration

Spring Integration

Part 1

Originals of Slides and Source Code for Examples:

http://courses.coreservlets.com/Course-Materials/spring.html

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

© 2008 coreservlets.com

For live Spring & Hibernate training, see

t htt //

l t

/

courses at http://courses.coreservlets.com/.

Taught by the experts that brought you this tutorial.

Available at public venues or customized versions

Available at public venues, or customized versions

can be held on-site at your organization.

C d l d d t ht b M t H ll

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

•Courses developed and taught by Marty Hall

–Java 5, Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF, Ajax, GWT, custom mix of topics

•Courses developed and taught by coreservlets.com experts (edited by Marty)

–Spring, Hibernate/JPA, EJB3, Ruby/Rails

(2)

Topics in This Section

p

Maven 2 build system overview

General build procedure

Multi-module project build process

Spring IoC integration

Testing with Maven 2 and Spring

Java EE training: http://courses.coreservlets.com

4

© 2008 coreservlets.com

B ild S t

(3)

Conventions

Variables

${KEY}

Home directory

${HOME}

Linux:

$HOME

or

~

Windows

%HOMEPATH%

Path separator

/

Linux - “

/

Windows – “

\

Java EE training: http://courses.coreservlets.com

6

General Goals

Manage source and configuration files

Distribute and execute in a test

environment

Integrate third-party libraries

Package and distribute project

Java EE training: http://courses.coreservlets.com

(4)

Maven 2 Build System

y

Implements best practices into build defaults

Comprehensive build system

Compile

Test

Test

Package

Distribute

Version

IDE integration

Report generation

Report generation

Customizable behavior

Java EE training: http://courses.coreservlets.com

8

Maven 2 Architecture

Maven client

Plugin execution framework

Project

Source tree

Source tree

Maven configuration

Repositoryy

Addressable (URL) file repository

Local repository cache

Pl i d

l d

Plugin downloads

Direct and transitive dependencies

(5)

Client Personalization

Global

Maven 2 installation root

conf/settings.xml

User

User

${HOME}/.m2/settings.xml

Java EE training: http://courses.coreservlets.com

10

Maven 2 Installation Process

Verify prerequisites

JDK 1 4+

– JDK 1.4+

– JAVA_HOME set to JDK installation root

Download maven

h // h /d l d h l ( 2 b)

– http://maven.apache.org/download.html (< 2mb)

– Maven 2.0.9 (zip/tar.gz/tar.bz2)

Unpack download

– Extract to target installation path

Set Maven 2 home

– Set variable M2_HOMEto the installation path

• Contains bin, boot, conf, and lib directories

Add

M2_HOME

bin directory to execution path

– Add ${M2_HOME}/binto PATHenvironment setting

Java EE training: http://courses.coreservlets.com

Verify repository settings

– Official, mirror, or corporate

(6)

Verify Repository Settings

y

p

y

g

Default

Official repository; a.k.a. central

http://repo1.maven.org/maven2

Mirror

Mirror

Traditional mirror

Redundant site

Redundant site

Corporate repository

Private network

Without internet access

Java EE training: http://courses.coreservlets.com

12

Repository Mirror

p

y

<settings> <mirrors> <mirrors> <mirror>

<id>mirror</id>

<name>mirror of central</name> <url>http://mirror/maven2</url> <mirrorOf>central</mirrorOf> </mirror>

(7)

© 2008 coreservlets.com

B ild P

Build Process

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

General Approach

pp

Compile source

Reference third-party libraries

Reference peer modules

Integrate project with IDE

Integrate project with IDE

Test binaries

Package source for distribution

Package source for distribution

Java EE training: http://courses.coreservlets.com

(8)

Process

Create project structure

j i j

project/src/main/java

project/src/main/resources

project/src/test/java

project/src/test/resources

project/src/test/resources

Create

project/pom.xml

Register Maven 2 NS

g

Define project meta-data

e.g. project identifiers

Pl i

tti

Plugin settings

Specify dependencies

Third-party libraries

Java EE training: http://courses.coreservlets.com

Local projects

16

Process Continued

Integrate with IDE

Prepare for eclipse

mvn eclipse:eclipse

• Plugins available for NetBeans and IntelliJ

Refresh or import project into IDE

p

p j

Develop source

src/main/java

e g l t M kC t D

• e.g., coreservlets.MockCustomerDao

Perform build activities

Compile, test, package, generate reports, and more…

p ,

, p

g , g

p

,

(9)

Create Project Structure

j

/coreservlets-api

/src /src

/main

/java

/resources /resources

/test /java

/resources /resources

Java EE training: http://courses.coreservlets.com

18

Create

pom.xml

/coreservlets-api

pom xml pom.xml

/src

/main

/java /java

/resources

/test /java /java

/resources

Java EE training: http://courses.coreservlets.com

(10)

Create

pom.xml

Register Maven 2 schema and namespace

i

mapping

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

</project>

Java EE training: http://courses.coreservlets.com

20

Create

pom.xml

Define project identifiers

Referenceable identifier from other projects

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>coreservlets</groupId> <groupId>coreservlets</groupId>

<artifactId>coreservlets-api</artifactId> <version>1.0-SNAPSHOT</version>

(11)

Create

pom.xml

Continued

Define compiler settings

<project> ...

<b ild> <build>

<plugins><plugin>

<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <artifactId>maven compiler plugin</artifactId> <version>2.0</version>

<configuration>

<source>1.5</source> <target>1.5</target> </configuration>

</plugin></plugins>

Java EE training: http://courses.coreservlets.com

</build> </project>

22

Create

pom.xml

Continued

Optional plugin customizations

<project> ... <b ild> <build>

<plugins><plugin>

<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <artifactId>maven surefire plugin</artifactId>

<configuration>

<useFile>false</useFile>

<trimStackTrace>true</trimStackTrace> <printSummary>true</printSummary>

<disableXmlReport>true</disableXmlReport> </configuration>

Java EE training: http://courses.coreservlets.com

</plugin></plugins> </build>

(12)

Create

pom.xml

Continued

Specify dependencies

<dependencies> <dependency>

< Id>l 4j</ Id> <groupId>log4j</groupId>

<artifactId>log4j</artifactId> <version>1.2.14</version>

</dependency> </dependency> </dependencies>

Java EE training: http://courses.coreservlets.com

24

Integrate with Eclipse IDE

g

p

Prepare project

mvn eclipse:eclipse

Clean configuration with

mvn eclipse:clean

Prepare Eclipse IDE

Prepare Eclipse IDE

From the Eclipse root window menu bar

Open

Window

/

Preferences

From

Preferences

Expand

Java

/

Build Path

/

Classpath Variables

/

New

From

From

New Variable Entry

New Variable Entry

Add

M2_REPO

mapped to local repository path

(13)

Integrate with Eclipse IDE

g

p

Import projects into Eclipse workspace

From the Eclipse root window menu bar

Open File / Import

From the Import wizard

From the Import wizard

Select / General / Existing Projects into Workspace / Next

Import Projects / Select root directory / Browse

From

Browse for Folder

Drill-down into project directory

Select

Select

OK

OK

Verify project is present and checked

Select Finish

Java EE training: http://courses.coreservlets.com

26

Develop Source

p

/coreservlets-api pom.xml

pom.xml /src

/main /java

/coreservlets

Customer.java CustomerDao.java

/resources /test

/java /

/resources

Java EE training: http://courses.coreservlets.com

(14)

Perform Build Activities

General build tasks

mvn clean

mvn compile

mvn test

mvn test

mvn package

mvn install

Optionally, specify multiple build tasks

mvn clean install

Java EE training: http://courses.coreservlets.com

28

Build Artifacts

Project

coreservlets-api

JAR

(15)

© 2008 coreservlets.com

Multi-Module

Multi Module

Projects

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

Multi-Module Project Structure

j

Project one

pom.xml

src/main/java

Project two

pom.xml

/

i /j

src/main/java

Project

N

l

pom.xml

src/main/java

pom xml

Java EE training: http://courses.coreservlets.com

pom.xml

(16)

Configuration

Parent

pom.xml

Module list

a.k.a. child projects

<modules><module/></modules>

<modules><module/></modules>

Parent settings

Plugin settings

Thi d

t lib

i

Third-party library versions

Global dependencies

Child

Child

pom.xml

pom.xml

Parent reference

<parent/>

Java EE training: http://courses.coreservlets.com

32

Parent

pom.xml

Parent project identifier and type

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www w3 org/2001/XMLSchema-instance" xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>coreservlets</groupId>

<artifactId>coreservlets-pom</artifactId> < i >1 0 SNAPSHOT</ i >

<version>1.0-SNAPSHOT</version>

(17)

Parent

pom.xml

Module list

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www w3 org/2001/XMLSchema-instance" xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

...

<modules>

<module>coreservlets-api</module> </modules>

</project>

Java EE training: http://courses.coreservlets.com

34

Parent

pom.xml

Plugin settings

Inherited by child projects

<project> ... <build>

<plugins> ... <plugin>

<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0</version>

<configuration>

<source>1.5</source> t t 1 5 /t t

Java EE training: http://courses.coreservlets.com

<target>1.5</target> </configuration>

</plugin> ...

(18)

Parent

pom.xml

Third-party library versions

<project> ...

<dependencyManagement> <dependencyManagement>

<dependencies> <dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId> <version>1.2.4</version>

</dependency> </d d i > </dependencies>

</dependencyManagement> </project>

Java EE training: http://courses.coreservlets.com

36

Parent

pom.xml

Third-party library versions

<project> ...

<dependencies> <dependencies> <dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId> </dependency>

(19)

Child project

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns http://maven.apache.org/POM/4.0.0

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion> <groupId>coreservlets</groupId>

<artifactId>coreservlets-api</artifactId> <artifactId>coreservlets-api</artifactId> <packaging>jar</packaging>

<name>coreservlets-api</name> <version>1.0-SNAPSHOT</version>

<parent>

<groupId>coreservlets</groupId>

<artifactId>coreservlets-pom</artifactId> i 1 0 SNAPSHOT / i

Java EE training: http://courses.coreservlets.com <version>1.0-SNAPSHOT</version>

</parent>

</project> 38

Build Artifacts

Project

coreservlets-pom

POM

coreservlets-pom-1.0-SNAPSHOT.pom

Project

Project

coreservlets-api

coreservlets api

JAR

coreservlets-api-1.0-SNAPSHOT.jar

Java EE training: http://courses.coreservlets.com

(20)

© 2008 coreservlets.com

Implementation

Implementation

Project

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

API Implementation Project

p

j

Parent configuration

pom.xml

Project

coreservlets-api

Project

coreservlets mock

(21)

Process

Create project structure for

coreservlets- mock

src/main/java

src/main/resources

src/main/resources

src/test/java

src/test/resources

Create

pom.xml

Specify dependencies

Thi d

t lib

i

Third-party libraries

• e.g., log4j

Local projects

Java EE training: http://courses.coreservlets.com

• e.g., coreservlets-api

42

Process Continued

Integrate with IDE

mvn eclipse:eclipse

Import or refresh project

Develop source

Develop source

src/main/java

coreservlets.MockCustomerDao

Perform build activities

e.g.,

mvn clean install

Java EE training: http://courses.coreservlets.com

(22)

Create

pom.xml

Project identifers

Parent project reference

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>coreservlets</groupId>

<artifactId>coreservlets-mock</artifactId>

<packaging>jar</packaging> <packaging>jar</packaging> <name>coreservlets-mock</name>

<version>1.0-SNAPSHOT</version>

<parent>

<groupId>coreservlets</groupId>

<artifactId>coreservlets-pom</artifactId> i 1 0 S S O / i

Java EE training: http://courses.coreservlets.com <version>1.0-SNAPSHOT</version>

</parent>

</project> 44

Create

pom.xml

Local project dependency

<project> ...

<dependencies> <dependencies> <dependency>

<groupId>${project.groupId}</groupId> <artifactId>coreservlets-api</artifactId> <version>${project.version}</version> </dependency>

</dependencies></project> </ j t>

(23)

Complete Process

p

Integrate with IDE

mvn eclipse:eclipse

Import or refresh project

Develop source

Develop source

src/main/java

coreservlets.MockCustomerDao

Perform build activities

e.g.,

mvn clean install

Java EE training: http://courses.coreservlets.com

46

Build Artifacts

Project

coreservlets-pom

POM

pom.xml

Project

Project

coreservlets-api

coreservlets api

JAR

coreservlets-api-1.0-SNAPSHOT.jar

P j t

Project

coreservlets-mock

JAR

coreservlets-mock-1.0-SNAPSHOT.jar

coreservlets mock 1.0 SNAPSHOT.jar

Java EE training: http://courses.coreservlets.com

(24)

© 2008 coreservlets.com

D

l

t

Deployment

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

Deployment Project

p y

j

Parent configuration

pom.xml

Project

coreservlets-api

Project

coreservlets mock

Project

coreservlets-mock

(25)

Process

Create project structure

src/main/java

src/main/java

src/main/resources

src/test/java

src/test/resources

Create

pom.xml

Register Maven 2 NS

Define project meta data; e g project identifiers

Define project meta-data; e.g. project identifiers

Plugin settings

• maven-jar-plugin

• maven-assembly-plugin – Specify dependencies

• Third-party libraries

Java EE training: http://courses.coreservlets.come.g., spring-core, spring-beans, spring-context

• Local projects

e.g., coreservlets- api, coreservlets-mock

50

Process Continued

Integrate with IDE

Prepare for eclipse

mvn eclipse:eclipse

• Plugins available for NetBeans and IntelliJ

Refresh or import project into IDE

p

p j

Develop source

src/main/java

e g l t M kC t D

• e.g., coreservlets.MockCustomerDao

Perform build activities

Compile, test, package, generate reports, and more…

p ,

, p

g , g

p

,

e.g.,

mvn clean install

Java EE training: http://courses.coreservlets.com

(26)

Create

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns http://maven.apache.org/POM/4.0.0

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

d l i 4 0 0 / d l i <modelVersion>4.0.0</modelVersion>

<groupId>coreservlets</groupId>

<artifactId>coreservlets-main</artifactId>

<packaging>jar</packaging> <packaging>jar</packaging> <name>coreservlets-main</name>

<version>1.0-SNAPSHOT</version>

<parent>

<groupId>coreservlets</groupId>

<artifactId>coreservlets-pom</artifactId> <version>1 0 SNAPSHOT</version>

Java EE training: http://courses.coreservlets.com <version>1.0-SNAPSHOT</version>

</parent>

</project> 52

Create

pom.xml

Project identifers

Parent project reference

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>coreservlets</groupId>

<artifactId>coreservlets-main</artifactId>

k i j / k i <packaging>jar</packaging> <name>coreservlets-main</name>

<version>1.0-SNAPSHOT</version>

<parent>

<groupId>coreservlets</groupId>

(27)

Create

pom.xml

Local project dependencies

<dependencies>

<dependency>

<groupId>${project.groupId}</groupId>

<artifactId>coreservlets-api</artifactId> <version>${project.version}</version> </dependency>

<dependency>

<groupId>${project.groupId}</groupId>

i i

<artifactId>coreservlets-mock</artifactId> <version>${project.version}</version>

</dependency>

/d d i

Java EE training: http://courses.coreservlets.com

</dependencies>

54

Create

pom.xml

Third-party dependencies

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring beans</artifactId> <artifactId>spring-beans</artifactId> </dependency>

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency>

<dependency>

d i f k / d

<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency>

<dependency>

Java EE training: http://courses.coreservlets.com

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId> </dependency>

(28)

Create

pom.xml

Customize plugins

maven-jar-plugin

Executable JAR

maven assembly plugin

maven-assembly plugin

Aggregate artifact with dependencies

Java EE training: http://courses.coreservlets.com

56

Complete Process

p

Integrate with IDE

mvn eclipse:eclipse

Import or refresh project

Develop source

Develop source

src/main/java

coreservlets.Main

Perform build activities

(29)

Build Artifacts

Project

coreservlets-pom

POM

pom.xml

Project

Project

coreservlets-api

coreservlets api

JAR

coreservlets-api-1.0-SNAPSHOT.jar

P j t

Project

coreservlets-mock

JAR

coreservlets-mock-1.0-SNAPSHOT.jar

coreservlets mock 1.0 SNAPSHOT.jar

Java EE training: http://courses.coreservlets.com

58

Build Artifacts Continued

Project

coreservlets-main

JAR

coreservlets-main-1.0-SNAPSHOT.jar

Assemblyy

coreservlets-api-1.0-SNAPSHOT.jar

coreservlets-mock-1.0-SNAPSHOT.jar

coreservlets-main-1 0-SNAPSHOT jar

coreservlets main 1.0 SNAPSHOT.jar

(Executable JAR)

spring-beans-2.5.5.jar

spring context 2 5 5 jar

spring-context-2.5.5.jar

spring-core-2.5.5.jar

aopalliance-1.0.jar

l

i

1 0 4 j

Java EE training: http://courses.coreservlets.com

commons-logging-1.0.4.jar

log4j-1.2.11.jar

(30)

© 2008 coreservlets.com

I t

t S i

I C

Integrate Spring IoC

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

General Approach

pp

Project

coreservlets-api

Project

coreservlets-mock

Include Spring bean definitions with build package

coreservletsContext xml

coreservletsContext.xml

Project

coreservlets-main

Define Spring bean definitions reference

Load bean definitions at runtime

Reference

coreservletsContext.xml

Specify Spring JAR dependencies

Specify Spring JAR dependencies

(31)

Project Structure

j

Project coreservlets-pom

pom.xml

Project

coreservlets-api

src/main/java

src/main/java

coreservlets.Customer

coreservlets.CustomerDao

Project

coreservlets-mock

pom.xml

/

i /j

src/main/java

coreservlets.MockCustomerDao

src/main/resources

Java EE training: http://courses.coreservlets.com

coreservletsContext.xml

62

Project Structure Continued

j

Project

coreservlets-main

pom.xml

• Spring IoC JAR references

src/main/java

s c/ a

/java

coreservlets.Main

Java EE training: http://courses.coreservlets.com

(32)

Project

j

coreservlets-mock

Add Spring bean definitions

src/main/resources

coreservletsContext.xml

<?xml version="1.0" encoding="UTF-8"?> <beans>

<bean id="customerDao"

class "coreservlets MockCustomerDao"> class="coreservlets.MockCustomerDao"> <constructor-arg>

<list>

<bean class="coreservlets.Customer"> <property name="id" value="jjoe" />

<property name="name" value="Java Joe" /> </bean>

Java EE training: http://courses.coreservlets.com

</list>

</constructor-arg> </bean>

</beans> 64

Project

j

coreservlets-main

Spring IoC JAR dependencies

coreservlets-main/pom.xml

<project> ...

<dependency>

<groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <artifactId>spring beans</artifactId> </dependency><dependency>

<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency><dependency>

<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency><dependency>

(33)

Project

coreservlets-main

Driver class

coreservlets.Main

public class Main {

public static void main(String[]args){

System.out.println("Searching for: " + args[0]);

BeanFactory factory = new ClassPathXmlApplicationContext( "/coreservletsContext.xml");

CustomerDao service =

(CustomerDao) factory.getBean("CustomerDao");

i ( [0])

Customer customer = service.getCustomerByName(args[0]); System.out.println("Found: " + customer);

} }

Standard output

Java EE training: http://courses.coreservlets.com

}

66

> java -jar coreservlets-main.jar "Java Joe" Searching for: Java Joe

Found: Customer id=jjoe, name=Java Joe

© 2008 coreservlets.com

Spring Library

Spring Library

Integration

Customized Java EE Training: http://courses.coreservlets.com/

(34)

General Approach

pp

Spring IoC Container

Deployment contexts

Test contexts

Spring AOP Libraries

Advisor code

l

Deployment contexts

Test contexts

Spring service abstractions

Spring service abstractions

Implementation projects

Java EE training: http://courses.coreservlets.com

68

Compilation Dependencies

Example

Example

coreservlets-api

coreservlets-mock

l t

i

coreservlets-api

coreservlets-jdbc

spring-jdbc, spring-tx

spring jdbc, spring tx

coreservlets-jpa

spring-orm, javax-persistence

coreservlets-advice

aopalliance, spring-aop

coreservlets main

(35)

© 2008 coreservlets.com

Testing with Maven 2

Testing with Maven 2

and Spring

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

Build Artifacts

Project

coreservlets-pom

POM

pom.xml

Project

Project

coreservlets-api

coreservlets api

JAR

coreservlets-api-1.0-SNAPSHOT.jar

P j t

j

Project

coreservlets-jdbc

JAR

coreservlets-jdbc-1.0-SNAPSHOT.jar

coreservlets jdbc 1.0 SNAPSHOT.jar

Java EE training: http://courses.coreservlets.com

(36)

Project Structure

j

Project coreservlets-pom

l

pom.xml

Project

coreservlets-api

src/main/java

• coreservlets.Customer

• coreservlets.CustomerDao

Project

Project

coreservlets-jdbc

coreservlets jdbc

pom.xml

src/main/java

l t S i Jdb C t D

• coreservlets.SpringJdbcCustomerDao

src/main/resources

• coreservletsContext.xml

Java EE training: http://courses.coreservlets.com

src/test/java

• ?

72

General Approach

pp

Create test class

Integrate Spring IoC container

Inject test fixture(s)

(37)

Process

Register Spring test JAR

org springframework spring test (test scope)

org.springframework:spring-test (test scope)

Create test class

– Name class with suffix Test

Pl l d j

– Place class under src/test/java

Create test resources

– Place test resources under src/test/resources

– e.g., applicationContext.xml defining platform beans such as dataSource

Integrate Spring IoC container (Spring TestContext)

– Class annotation

@RunWith(SpringJUnit4ClassRunner.class) – Class annotation

Java EE training: http://courses.coreservlets.com

• @ContextConfiguration(

locations={"path-0","path-N"})

74

Process Continued

Inject test fixtures

@Autowired

fields or methods

• By type

• Optional qualifier

@Resource

fields or methods

• By bean name

Define lifecycle methods (optional)

Define lifecycle methods (optional)

@BeforeClass

,

@Before

,

@After, @AfterClass

Define test cases

@Test

methods

Execute tests

t st

Java EE training: http://courses.coreservlets.com

mvn test

• Or, a goal with a test goal dependency; e.g., mvn install

(38)

Create Test Resources

src/test/resources

coreservletsTestContext.xml

<beans>

<bean id="dataSource"

class="coreservlets.EmbeddedDerbyDataSource"> <constructor-arg value="target/ngcdb" />

<constructor-arg> <list>

<value>/setup.sql</value> </list>

</list>

</constructor-arg> </bean>

</beans>

Java EE training: http://courses.coreservlets.com

</beans>

76

Create Test Resources

Continued

Continued

src/test/resources

setup.sql

create table customer ( id varchar(36) not null, name varchar(32) not null, primary key (id),

i

unique (name) );

delete from customer;

insert into customer (id name) values ('jjoe' 'Java insert into customer (id, name) values ('jjoe','Java

Joe');

insert into customer (id, name) values ('jjohn','Java John');

(39)

Create Test Class

src/test/java

coreservlets/CustomerDaoTransactional

Test

package coreservlets;

public class CustomerDaoTransactionalTest {

}

Java EE training: http://courses.coreservlets.com

78

Create Test Class Continued

Reference bean definition paths

src/test/resources

coreservletsContext.xml

coreservletsTestContext.xml

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(g

locations={"/coreservletsContext.xml",

"/coreservletsTestContext.xml"}) public class CustomerDaoIntegrationTest {

}

Java EE training: http://courses.coreservlets.com

(40)

Integrate Spring IoC Container

g

p

g

@RunWith

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={

/ l l

"/coreservletsContext.xml",

"/coreservletsTestContext.xml"}) public class CustomerDaoIntegrationTest {

} }

AbstractJUnit4SpringContextTests

i i i

@ContextConfiguration(locations={

"/coreservletsContext.xml",

"/coreservletsTestContext.xml"}) public class CustomerDaoIntegrationTest

Java EE training: http://courses.coreservlets.com

public class CustomerDaoIntegrationTest extends AbstractJUnit4SpringContextTests { }

80

Inject Test Fixture with

@Autowired

@Autowired

Field injection

i i

public class CustomerDaoIntegrationTest {

@Autowired

public CustomerDao service; }

}

Setter injection

@Autowired

public void setService(CustomerDao service){ this.service = service;

}

Q

lifi d

tt

i j ti

Qualified setter injection

@Autowired

public void setService(

(41)

Inject Test Fixture with

@Resource

@Resource

Field injection

i i

public class CustomerDaoIntegrationTest {

@Resource

public CustomerDao service; }

}

Setter injection

@Resource

public void setService(CustomerDao service){ this.service = service;

}

Q

lifi d

tt

i j ti

Qualified setter injection

@Resource(name="beanName")

public void setService(CustomerDao service){

Java EE training: http://courses.coreservlets.com

this.service = service; }

82

Create Lifecycle Methods

(Optional)

(Optional)

public class CustomerDaoIntegrationTest { ...

...

@BeforeClass

public static void execOnceBeforeAllTestCases(){ }

@Before

public void execBeforeEachTestCase(){ }

@Test

public void testMethod(){ } public void testMethod(){ } @Test

public void testDemo01(){ }

@After

public void execAfterEachTestCase(){ }

@AfterClass

bli t ti id Aft AllT tC (){ }

Java EE training: http://courses.coreservlets.com

public static void execAfterAllTestCases(){ } }

(42)

Create Test Cases

package coreservlets;

import static org.junit.Assert.*;

public class CustomerDaoIntegrationTest {

@Test

public void testCase(){

assert service != null : "Service is null";

assertNotNull("Service is not null", service);

} }

Java EE training: http://courses.coreservlets.com

84

Execute Test Cases

---T E S ---T S

T E S T S

---Running coreservlets. CustomerDaoIntegrationTest

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time l d 0 159

elapsed: 0.159 sec

Results :

(43)

© 2008 coreservlets.com

W

Wrap-up

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

Summaryy

Maven 2

Automates best practices

Multi-module project builds

coreservlets-api

coreservlets-mock

coreservlets-jdbc

coreservlets-main

Spring library integration

Isolated to the deployment context

• <dependencies><dependency/></dependency>

Java EE training: http://courses.coreservlets.com

(44)

Summary Continued

y

Testing with Maven 2 and Spring

Integrate container

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={

})

@ContextConfiguration(locations={...})

Inject test fixtures

@Autowired

or

@Resource

Test cases

@Test

Execute tests

Execute tests

mvn test

Java EE training: http://courses.coreservlets.com

88

© 2008 coreservlets.com

Q

ti

?

References

Related documents

The cell e.s.d.'s are taken into account individually in the estimation of e.s.d.'s in distances, angles and torsion angles; correlations between e.s.d.'s in cell parameters are

The leaves of the tested cultivars, irrespective of the level of resistance to filbert aphid, showed a definitely higher concen- tration of acids, derivatives of trans -cinnamic

The lowest damage was observed in the treatment with Biscaya 240 OD (0.05 boreholes/ capsule) and Spintor (0.08 boreholes/ capsule), and there was a highly significant difference

complementary base pairing involves 2-amino group and ring N3 i atom of inversion related pyrimidine moiety of AMPY. These discrete

The increase of germination percentage and germi- nation rate with the rise of temperature demonstrated that seeds were incubated in the range of suboptimal temperatures for

The objectives of this study were to ( i ) investigate the effect of different concentrations of several fungicides commonly used in the Czech Republic to control Sclero- tinia

The ethoxycarbonyl unit is present in a synperiplanar conformation with respect to the pyrrole ring, as indicated by the dihedral angle of 14.5 (3).. In

The cell e.s.d.'s are taken into account individually in the estimation of e.s.d.'s in distances, angles and torsion angles; correlations between e.s.d.'s in cell parameters are