Article Unit Tests and IBM Cloud DevOps

Unit tests and IBM CLoud DevOps Technical and business point of view

Written by Diego Sanchez Schenone

Introduction This article is meant to share my experience about two important aspects of the development process to be kept in mind if you want to improve the quality of your outcomes.

Unit Tests and are pillars of the development process which provide a ​ ​ solid foundation to deliver products and services of quality. They help and improve in many ways the methodology we produce software.

Another important subject to be presented is “DevOps”. In the last years we heard a lot about this term. From my point of view, “DevOps” is the right approach to improve the process we develop software and it should be implemented by software companies that want or need to enhance their development process.

The article is divided in three parts. Firstly, we are going to discuss what are unit tests and which benefits they bring to us. Secondly, we will show how to use JUnit to implement unit tests in Java language. Finally, we are going to have a first look about IBM Cloud DevOps and tell our real experience of using it.

Part1: What is a unit test Unit testing can be defined as a kind of which is focused on individual unit components of a software program. These units must be the smallest testable components.

Depending on the programming paradigm these units can be methods if we talk about the object oriented programing, or functions in the functional paradigm. They must have the characteristic of being locally isolated to be tested (must not be related to another components).

In this testing model, we usually group a set of these components conforming a Test Suite ​ which bundles tests that are locally related.

An important question to be asked is: When do they must be coded?. And the answer is simple: As the develop of the software components grows simultaneously unit tests must be designed and implemented for each of them. Based on our experience they must not put off for the end because it is too difficult to do it and purposeless (we need to tests what we develop as soon as possible).

Technologies for Business - Article Unit Tests and IBM Cloud DevOps

Which benefits do they bring to us? Unit tests bring many benefits to the process which make it controllable and measurable, among others we have the following.

❏ Check if the methods/functions work as expected.

❏ Identify failures in the implementation of the algorithms.

❏ Covering more user scenarios by creating test suites.

❏ It is a “must” to improve and control the quality of the product.

❏ They are an essential part to implement and Delivery. ​

Feature Benefits

Software Development Business Process

Unit test Check and measure every Measure the product. If single component from the something can be measured then very beginning. can be managed.

Test Suites More testing scenarios are Offer a higher quality product ​ added gradually according because more use cases are to the software is growing. covered to be tested.

Continuous Delivery Automatization Cost reduction Manual and non values tasks are removed from the process.

Be more competitive because developing times are optimized given this way a fast response to the business needs. Benefits of implementing unit testing Quick recap

● How big the size of the component to be tested should be? The smallest component such as function, method or procedure. ● What is a Test Suite? A group of unit tests that are tested together. ● When do they must be coded? Just after finished a software component. ● When do they must be executed? After any change has been added. ● How often they be executed? They should be a part of Continuous Delivery therefore executed after any building.

Diego Sanchez Schenone

1 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

Part 2: JUnit JUnit is a framework designed to write and run unit tests for the Java language. It provides lots of good features to build unit tests. Through a simple but helpful example we are going to explain the main concepts and features of JUnit.

The software to be tested We are in charge of developing two classes which represent arrays of numbers. This components are part of a Math system that runs online in a web application.

Class Description

NumberArray This class represents an Array that only can contain integer numbers and does not allow repeated elements. It has the following methods:

public boolean addValue(Integer value) Add the argument in the array If it does not contain it.

public void removeAllElements() Remove all elements.

public boolean removeValue(Integer ) Remove the argument from the array.

public boolean containsValue(Integer value) Return true if the argument belongs to the array.

public boolean isEmpty() Return true is the array is empty, otherwise false.

NumberRangeArray This class extends NumberArray. It overrides the method addValue to control if the argument is between a given range. This class has two attributes minValue and maxValue to set the ​ ​ valid range of its elements.

public boolean addValue(Integer value) It controls if the argument is between the minValue and maxValue to be added into the array. If not it returns false.

Immediately after we developed them we write the tests to verify if the methods respond as we expected.

We are going to develop the following classes to test them. In JUnit “a test” is represented by a method using the Java annotation @Test. ​

Diego Sanchez Schenone

2 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

Class Description

NumberArrayTest In this class we develop the following testing methods.

@Test public void testIsEmpty() If checks if the array is empty.

@Test public void testAddValueOk() It verifies if a number that not belongs to the Array is added.

@Test public void testAddValueNotOk() It verifies if a number that belongs to the Array is not added when is intended to be added.

NumberRangeArrayTest To test the class NumberRangeArray we are going to develop the following tests. Each has the responsibility the only check one method.

@Test public void testIsEmpty() If checks if the array is empty.

@Test public void testAddValueOk() It verifies if a number that is in range (between minValue and maxValue) is added.

@Test public void testAddValueNotOk() It verifies if a number that is not in range (between minValue and maxValue) is not added.

In addition, because these components are logically related we develop a Test Suite to group them.

Test Suite Description

ArrayTestSuite It groups both classes NumberArrayTest and NumberRangeArrayTest.

Diego Sanchez Schenone

3 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

The JUnit code We are going to take a look at this code fragment to see the main concepts of JUnit which were implemented.

package com.tfb.learning..test1;

import org.junit.Test; import org.junit.After; import org.junit.Before; import static org.junit.Assert.assertEquals;

import com.tfb.learning.junit.app.NumberArray;

public class NumberArrayTest {

@Before ​ public void setInitialState() { System.out.println("1 - Setting the array to empty"); NumberArray.getInstance().removeAllElements(); }

@After ​ public void setFinalState() {

System.out.println("After executing all tests"); }

@Test ​ public void testIsEmpty() {

NumberArray.getInstance().removeAllElements(); assertEquals(true,NumberArray.getInstance().isEmpty());

…. }

Sometimes we need to be sure that several conditions should be met before the test execution. In this example, before the test begins, we set the array to empty. JUnit provides the annotation @Before which indicates that a given method is the first to ​ be run. The same is when all tests are done by using the annotation @After. At the ​ ​ end, after all test were executed the method with this annotation is run.

By using the annotation @Test we indicates that a given method is a test. This ​ ​ annotation is interpreted by the Junit framework at the moment of the running.

Diego Sanchez Schenone

4 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

To verify if the result of a method is the expected Junit provides lots of static methods in the class Assert. In the method testIsEmpty() it was used the following ​ ​

assertEquals(true,NumberArray.getInstance().isEmpty()); ​ ​ ​ to check if the result of NumberArray.getInstance().isEmpty() is true. We have to use these static methods to corroborate the test results due to they are part of the framework.

The following is the suite which contains the tests for both classes.

package com.tfb.learning.junit.test1;

import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class) @SuiteClasses({ NumberArrayTest.class, NumberRangeArrayTest.class }) public class ArrayTestSuite {

}

As we can see by using the annotation @SuiteClasses we declare which classes are ​ ​ part of the suite.

Finally, the annotation @RunWith(Suite.class) indicates which class is going to run ​ ​ all test classes. The org.junit.runners.Suite class is provided by the Junit Framework

The The entire project can be downloaded from Bitbucket at https://[email protected]/dmsanchezschenone/public-tfb-learning-junit.git

It was used Maven to develop the Web application that contains these methods. To compile and execute the tests on the components the following commands must be run: mvn compile war:war mvn test

Diego Sanchez Schenone

5 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

From https://maven.apache.org/download.cgi you can download the lasted version ​ ​ of the framework.

Part 3: DevOps and IBM Cloud

What is DevOps? As far as I'm concerned the software development process consists of lots of tasks that are closely related to get a common goal (the link that bring all them together). These tasks are from different nature because they belong to separate and usually completely distinct areas or business lines which are working on this common goal. At glance we can have the following ones:

● The client ● The business ● IT Development Team ● IT Operations

Even more, we can explode in detail every of them and get more tasks which are performed by different roles inside of each area, for instance:

The development team

● Project managers ● Project leaders ● Architects ● Functional analysts ● Developers of flavors like backend, gui, integration, etc. ● Testers ● UI Designers

All these roles add their working quota to produce the final outcome of the development team. They need to be coordinated, communicated, managed and measured to be sure they are doing what is expected. I can say this last thing is pretty hard to achieve it if we do not count on a methodology, process, and moreover, in an automate way when it is possible.

We can image all tasks that these roles perform are in a big pipeline, like a production line. This pipeline must be well designed, optimized and finally automized.

Diego Sanchez Schenone

6 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

I would like to point out that during my career I have seen the lack of cooperation between the main areas of an organization. For instance, it is usual that the development and operational teams work separately and even worse, they work with some kind of rivality. I have worked on different sides of the IT field such as development, operational, consulting and sales, and I could see how the differences among them bring a poor outcome that at then end it is the client who pays (or overpay) for this.

It is crucial to have an approach to bring all these areas together specially the Development and Operations areas of IT.

DevOps (Development and Operations) is a “practice” which brings them together to ​ ​ ​ improve the development process to.

● Reduce development times. ● Improve the quality of the outcomes. ● Get rapid feedback from the users. ● Provide a continuous delivery and monitoring.

It is an interdisciplinary practice due to it combines differents roles through the whole ​ process, for instance for a given project we can have the following delivery pipeline

Developing Building and QA User Acceptance PROD Deploy Test

Devs Ops Devs Devs+Users Ops

On this simple project we can see how the Devs and Ops areas are working together and at least we have the following roles working on it.

● Developers ● IT Operators for building and deploy. ● Testers ● Users

This basic project can be extended to this new scenario which is a bit more complex

Developing Building Unit Integration UAT Issue PROD Monitoring and Deploy Testing Testing Management.

As more disciplines and roles are part of the process we need the right tools for every one.

Diego Sanchez Schenone

7 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

In DevOps we have the concept of “Toolchains” because in this practice we do not ​ ​ have only one tool to do the task, we have a set of tools to do lots of tasks. Each is oriented to a specific discipline but they are linked by the delivery pipeline.

Some example of tools are: git repositories (git, bitbucket), testing tools (selenium, maven) , building tools (jenkins, maven), issue management (PagerDuty), developing tools (Eclipse Orion Web IDE), among many others.

To summarize DevOps is a “practice” that brings the areas of Development and ​ ​ Operations together. This is implemented by a working pipeline where all tasks are linked and ordered. The ultimate goal is to improve the final product by achieving the following benefits. IT Area Business

Reducing the development times. Rapid respond to the customers ​ needs.

Improving the quality of the product A higher quality product in ​ because bugs are detected in early comparison to competitors. stages and quickly.

Continuous delivering and monitoring. Cost reduction because manual tasks ​ have been removed by the automatization.

Measure the quality of the service or product.

Count on a methodology which part of it Cost reduction is automatized. Reduce the learning curve for new employees.

What is IBM Cloud? It is a platform based on open standards and cloud to build, deploy, manage and run applications. It is a development environment where developers can use all services it provides to build applications for the cloud.

As an IBM platform we can use many of IBM products and services as well as third party providers and open standards. It means that it is no mandatory to use all IBM products to develop or run an already developed application (if we are migrating from another platform) on it.

Diego Sanchez Schenone

8 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

DevOps is offered like part of the IBM Cloud platform and we can find all concepts that were mentioned previously implemented for real.

Well, now I will share my experience of using IBM Bluemix DevOps. Like an owner of a software startup I have been faced many challenges to solve (I still do). Many of them are the same that big companies have and some are particulars of having an startup, like the financial backing the big ones have.

In a small company to be competitive there is no option to provide either a good service or product at a competitive price with quality levels of that justifies the price to be paid (among many others more). Startups have short margins and there is no way to waste time and make mistakes if we want to have a profitable business.

From my point of view, the only way to reduce the risks and have a project between some reasonable control margins is by implementing an automated process. Saying “automated” implies that many things have been done before. The process had to have been thought, designed, tested, documented, and finally measured. Only at the end of doing this we can automate something if not we are going to automate an inefficient process.

By implemented IBM Cloud DevOps we could automate tasks, reduce deployment and testing times, optimized costs and make the process to flow more naturally. From the technical point of view we did the following: 1. Manage the source code by using Bitckucket. 2. Automatic deployment after a build was completed. 3. Test automatization for components and REST services after deployments. 4. Detect issues immediately after a version component was release. 5. Automatic reporting. 6. Monitoring.

IBM Cloud DevOps offers many tools and services, particularly we have been used: - Bitbucket - Git - Maven for building and testing - IBM Cloud Monitoring services. - IBM Cloud Notification services. All our applications run on the cloud like cloud foundry applications on the runtime ​ environments of NodeJs, Tomcat and WebSphere Liberty. ​

All these technical improvements are translated in the following business ​ ​ benefits. ​

Diego Sanchez Schenone

9 Technologies for Business - Article Unit Tests and IBM Cloud DevOps

● Elimination of manual and tech tasks: we focus on the business solution only. ​

● No need to have operational roles: like an software development startup it is ​ difficult to afford more roles at the beginning. With DevOps we can operate without them (nothing personal guys, I have been one of you :) ).

● Rapid development: because we have all in a delivery pipeline where all tasks ​ are tigregged after a commit in the repository.

● Reduce of the learning curve: Junior roles do not have the need to learn tons ​ of concepts which only the years of practicing can teach you.

My point of view is that DevOps can help companies of both worlds: the small and big ones. Each world faces its own problems but many of them are common in both.

From working for big companies where lot of teams and roles play, I have been learned and suffered how the lack of cooperation, organization and automazition between Devs and Ops ended in lost of money (or less profit), and something which is even much worse lost of the credibility by the customer.

I do believe that this approach it the right one to improve our discipline (I mean, as IT professionals) and I do encourage you to start to think about implementing it. All of us perfectly know that changes are painful and make us feel uncomfortable. However through them we can make the progress to have a better business and offer some extra value to whom are the reason we are here: our customers.

About the author Diego Sanchez has been working in the software industry like a Java , Software Architect, Middleware Admin, Project Leader and Technology Consultant since 2002. He has worked for companies such as IBM, Oracle and worked primarily on bank and retail industries. During his career as consultant he has traveled around Latin America working for most important banks of Argentina, Uruguay, Chile, Peru and Ecuador. The most Diego loves is teaching, writing and traveling. He has been a teacher for many years specialized on software development and middleware technologies.

Since September 2016 he is founder of Technologies for Business FZE starting a new career like an entrepreneur. You can reach Diego at [email protected]. ​

Diego Sanchez Schenone

10