Continuous integration

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: 

Let's try this out:

  1. The most important file in a Maven-based project is pom.xml (POM or Project Object Model).
  2. 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>
  1. 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:
  1. 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;
}

}
  1. 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());
}
}
  1. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset