maven and j unit introduction

25
Maven & JUnit introduction

Upload: sergii-fesenko

Post on 16-Apr-2017

41 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Maven and j unit introduction

Maven & JUnit introduction

Page 2: Maven and j unit introduction

Agenda

● Introduction

● Build lifecycle

● pom.xml

● Directory layout

● Archetypes

● JUnit

Page 3: Maven and j unit introduction

Introduction

● Introduction

● Build lifecycle

● pom.xml

● Directory layout

● Archetypes

● JUnit

Page 4: Maven and j unit introduction

Introduction

How to compile java project

● Use command line and javac

● Use your IDE

● Use make

● Use ANT (Another Neat Tool)

● Use Maven

● Use Gradle

Page 5: Maven and j unit introduction

Introduction

Why maven?

● De facto standard

● Able to compile, test, pack, distribute source code

● Robust dependency management

● Extensible via plugins

● Large community (there are a lot of different plugins and solutions

for maven)

Page 6: Maven and j unit introduction

Build lifecycle

● Introduction

● Build lifecycle

● pom.xml

● Directory layout

● Archetypes

● JUnit

Page 7: Maven and j unit introduction

Build lifecycle

● Maven relies on build lifecycle to define the process of building and

distributing artifacts (jar, war, ear, etc)

● There are 3 build-in lifecycles

○ Default (handles project building and deployment)

○ Clean (handles project cleaning)

○ Site (handles project site documentation generation)

Page 8: Maven and j unit introduction

Lifecycle phases

https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

Validate Compile Test Package Integration test Verify Install

Deploy

Page 9: Maven and j unit introduction

Lifecycle phases

● You can call specific phase on the lifecycle (ex: mvn test)

● It will execute not only that build phase, but also every build phase

prior to the called build phase.

Validate Compile Test

Page 10: Maven and j unit introduction

pom.xml

● Introduction

● Build lifecycle

● pom.xml

● Directory layout

● Archetypes

● JUnit

Page 11: Maven and j unit introduction

pom.xml

● POM stands for Project Object Model. It is an XML file that always

resides in the base directory as pom.xml

● POM contains information about project, dependencies, and various

configuration details used by Maven to build the project(s).

Page 12: Maven and j unit introduction

pom.xml sample

<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>com.epam</groupId> <artifactId>unit-test-sample</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging>

<name>Maven jUnit sample</name>

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies></project>

Page 13: Maven and j unit introduction

pom.xml

<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>com.epam</groupId> <artifactId>unit-test-sample</artifactId> <version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

<name>Maven jUnit sample</name>

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies></project>

Page 14: Maven and j unit introduction

Directory Layout

● Introduction

● Build lifecycle

● pom.xml

● Directory layout

● Archetypes

● JUnit

Page 15: Maven and j unit introduction

Default directory layout

.

├── pom.xml

└── src

├── main

│ ├── java

│ │ └── com

│ │ └─ epam

│ │ └── unittest

│ │ └─ Main.java

│ └── resources

└── test

├── java

│ └── com

│ └─ epam

│ └─ unittest

│ └─ MainTest.java

└── resources

Page 16: Maven and j unit introduction

Default directory layout

.

├── pom.xml

└── src

├── main

│ ├── java

│ │ └── com

│ │ └─ epam

│ │ └── unittest

│ │ └─ Main.java

│ └── resources

└── test

├── java

│ └── com

│ └─ epam

│ └─ unittest

│ └─ MainTest.java

└── resources

This part is common for each java maven project

Page 17: Maven and j unit introduction

Archetypes

● Introduction

● Build lifecycle

● pom.xml

● Directory layout

● Archetypes

● JUnit

Page 18: Maven and j unit introduction

Maven archetype

What is archetype?

● archetype is maven plugin whose task is to create a project structure

as per its template

● It helps user to to quickly start a new java project using command

mvn archetype:generate

Quickstart archetype generates “Hello world” project with unit test

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Page 19: Maven and j unit introduction

JUnit

● Introduction

● Build lifecycle

● pom.xml

● Directory layout

● Archetypes

● JUnit

Page 20: Maven and j unit introduction

What is JUnit?

● JUnit is an open source framework that has been designed for the purpose of writing and running tests in the Java programming language

● Originally written by Erich Gamma and Kent Beck

● JUnit is a regression-testing framework that developers can use to write tests as they develop systems

Page 21: Maven and j unit introduction

How it looks

public class Sum {

int plus(int a, int b) { return a + b; }}

import org.junit.Test;public class MainTest {

@Test public void testPlus() throws Exception { Sum sum = new Sum(); assertEquals(4, sum.plus(2, 2)); }}

Page 22: Maven and j unit introduction

How it looks

public class MainTest { Sum sum;

@Before public void setUp() throws Exception { sum = new Sum(); }

@Test public void testPlus() throws Exception { assertEquals(4, sum.plus(2, 2)); }}

Page 23: Maven and j unit introduction

Maven and JUnit

● The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application.

● It generates reports in two different file formats:● Plain text files (*.txt)● XML files (*.xml)

● By default, these files are generated at ${basedir}/target/surefire-reports.

Page 24: Maven and j unit introduction

Maven and JUnit

mvn test

[INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building Maven jUnit sample 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO]…

[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ unit-test-sample ---[INFO] Surefire report directory: /work/java/unit-test-sample/target/surefire-reports

------------------------------------------------------- T E S T S-------------------------------------------------------Running com.epam.unittest.MainTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Page 25: Maven and j unit introduction

Questions ?