Here, you need to make your new code go to the main branch as soon as possible. This will guarantee that your code is building properly and that the tests are planned and executed successfully. You will achieve it by using Git, Maven, and JUnit:
- Git: I'll not get too deeply into how to use Git and its commands, as it's not the focus of this book. If you are completely new to the Git world, get started by looking at this cheat sheet: https://education.github.com/git-cheat-sheet-education.pdf.
- Maven: Maven is one of the most powerful tools I've ever seen and hence has a bunch of features embedded. If you are new to it, check out this reference: https://maven.apache.org/guides/MavenQuickReferenceCard.pdf.
Let's try this out:
- The most important file in a Maven-based project is pom.xml (POM or Project Object Model).
- For example, when you create a new Java EE 8 project, it should look like this:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.eldermoraes</groupId>
<artifactId>jakartaee8-project-template</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>jakartaee8-project-template</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
- Then, your project is ready for building using Maven like this (running in the same folder where pom.xml is located):
mvn
- JUnit: JUnit is used to run your unit tests. Let's check it:
- Here is a class to be tested:
public class JUnitExample {
@Size (min = 6, max = 10,message = "Name should be between 6 and 10
characters")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- Here is a testing class:
public class JUnitTest {
private static Validator VALIDATOR;
@BeforeClass
public static void setUpClass() {
VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
}
@Test
public void smallName(){
JUnitExample junit = new JUnitExample();
junit.setName("Name");
Set<ConstraintViolation<JUnitExample>> cv =
VALIDATOR.validate(junit);
assertFalse(cv.isEmpty());
}
@Test
public void validName(){
JUnitExample junit = new JUnitExample();
junit.setName("Valid Name");
Set<ConstraintViolation<JUnitExample>> cv =
VALIDATOR.validate(junit);
assertTrue(cv.isEmpty());
}
@Test
public void invalidName(){
JUnitExample junit = new JUnitExample();
junit.setName("Invalid Name");
Set<ConstraintViolation<JUnitExample>> cv =
VALIDATOR.validate(junit);
assertFalse(cv.isEmpty());
}
}
- Whenever you run the building process for this project, the preceding test will be executed and will ensure that those conditions are still valid.
Now, you are ready for continuous integration. Just make sure to merge your new and working code into the main branch as soon as possible. Now, let's move on to continuous delivery.