Using JUnit, Mockito for Java Unit Testing, Test Driven Development

Overview of Software testing

Software testing plays an important role in the life cycle of software development. It is imperative to identify bugs and errors during software development and increase the quality of the product.

Therefore, one must focus on software testing. There are many approaches and Test Driven Development approach is one of them. Test Driven Development is a key practice for extreme programming, it suggests that the code is developed or changed exclusively on the basis of the Unit Testing.


TDD (Test Driven Development)

Test-Driven Development (TDD) is a software development process which includes test-first development. It means that the developer first writes a fully automated test case before writing the production code to fulfil that test and refactoring. Steps for the same are given below -


BDD (Behaviour Driven Development)

Behaviour Driven Development (BDD) is similar to the Test Driven Development. In other words, Behaviour Driven Development is the extended version of Test Driven Development.

The process is similar to Test Driven Development. In this also, the code is first written in Behaviour Driven Development and the production code. But, the main difference is that in Behaviour Driven Development the test is written in plain descriptive English type grammar as opposed to Test Driven Development.

This type of development -

The major benefit of Behaviour Driven Development is that it can easily be understood by a non-technical person also.


What is a Legacy Code?

In simple words, a code without testing is known as legacy code. A big disadvantage of legacy code is that it’s not easily understandable (for both developments as well as the business team) and that is the only reason why it is difficult to change the code for new features.

Therefore, a code without tests is a bad code. It is necessary to write a code with tests for best productivity and easy modifications.

Why Test Driven Development?

Advantages of Test Driven Development

Let's take an example, assume that you write some code and then compile it and then test it and maybe there are chances of failure. In this case, it becomes easy to find and fix those defects if you've written two new lines of code than a thousand.

Basically, a Test Driven Development is -

Need for Test Driven Development


Test Driven Development with Java

In the Java community, Test Driven Development plays an important role in designing and implementation of a software/program. Test Driven Development helps the programmer in several ways, such as -

Using Test Driven Development concept in our programming skills -

The major problem with programmers is that, while programming whenever the code base gets bigger it becomes very difficult to change and debug the code because there is a high chance of the code getting messed up. But, if we are using Test Driven Development technique -

It becomes easy to view what the error is, where it is and how it is paralyzing our code.

You May also Love to Read Building Serverless Microservices With Java

API Testing

API Testing is one of the types of testing techniques. This type of technique is used in the following cases -

During API testing if the API returns a valid/correct response, then that API will say to have passed.

The outputs of an API testing are of three types -

API Testing can be done by -


Test Driven Development Tools

As discussed above, there are many tools available for testing and improving the overall design and implementation of the software system. Some of the most common testing tools are listed below -

JUnit for Unit Tests 

JUnit is a unit testing framework designed for Java programming language. Since unit tests are the smallest elements in the test automation process. With the help of unit tests, we can check the business logic of any class.

So JUnit plays an important role in the development of a test-driven development framework. It is one of the families of unit testing frameworks which is collectively known as the xUnit that originated with SUnit.

JMeter for Load/Performance Testing

Apache JMeter may be used to test performance both on static and dynamic resources, Web dynamic applications (Mainly for Load/Performance testing). Basically, it is used to simulate a heavy load on a server, group of servers, network or object to test its strength or to analyze overall performance under different load types.

Features of Apache JMeter -

Ability to load and performs tests on many different applications/server/protocol types, some of them are listed below -

Mockito for Rest API Testing

Mockito is designed as an open source testing framework for Java which is available under an MIT License. Mockito allows programmers to create and test double objects (mock objects) in automated unit tests for the purpose of Test-driven Development (TDD). In simple words, we can say that Mockito is a framework that we specifically use to efficiently write certain kind of tests.


Setting Up Environment For TDD 

Setting up TDD with JUnit

OR

If it’s not a Maven project then download the JUnit jar and include it in your project.

Setting Up TDD with JMeter

Setting Up TDD with Mockito


Implementing Test Driven Development

Unit Testing With JUnit

Production Code


public class Student {
 public String displayStudentName(String firstName, String lastName) {
  return firstName + lastName;
 }
}

Testing Code


Import org.junit.Test;
Import static org.junit.Assert.*;
Public class StudentTest {
 @Test
 Public void testDisplayStudentName() {
  Student student = new Student();
  String studentName = student.displayStudentName(“Anshuman”, ”Nain”);
  assertEquals(“AnshumanNain”, studentName);
 }
}

Now build the project and run

Unit Testing With JMeter

Let’s test a login API (POST Method) using JMeter for performance testing.

Below are the Steps for scripting the rest API -

Then finally run the test.

In above example, we hit 1000 requests per sec, out of which 992 were successful.

Unit Testing With Mockito

Basic Simple Demo code for using mockito with JUnit -

Production Code


public class TestService {
 public int getUniqueId() {
  return 43;
 }
}

Testing Code


public class JUnitServiceTestExample {

 @Test
 public void testGetInt() {
  // create mock
  TestService test = Mockito.mock(TestService.class);

  // define return value for method getUniqueId()
  when(test.getUniqueId()).thenReturn(43);

  // use mock in test....
  assertEquals(test.getUniqueId(), 43);
 }
}


Concluding Test Driven Development

In the end, it is safe to say that Test Driven Development must be adopted by as many developers as possible, in order increase their productivity and improve not only the code quality but also to increase the productivity and overall development of software/program. TDD also leads to more modularized, flexible and extensible code.

How Can Don Help You?

Don follows the Test Driven Development Approach in the development of Enterprise level Applications following Agile Scrum Methodology.

Application Modernization means re-platforming, re-hosting, recoding, rearchitecting, re-engineering, interoperability,  of the legacy Software application for current business needs. Application Modernization services enable the migration of monolithic applications to new Microservices architecture with Native Cloud Support including the integration of new functionality to create new value from existing application. 

Develop, Deploy and Manage Agile Java Application on leading Cloud Service Providers - Google Cloud, Microsoft Azure, AWS, and Container Environment - Docker and Kubernetes

Enable Secure Continuous Integration and Continuous Delivery Pipeline with Jenkins/Bamboo/TeamCity for delivery, automation, self-service environment, and On-demand. Deploy Java Application to production by decreasing the time by the automation of entire delivery pipeline comprising build, deploy, test, and release.

Take a cloud-native approach to building Enterprise Applications for Web and Mobile with a Microservices Architecture. An Application based on Microservices Architecture is Small, Messaging–enabled, Bounded by contexts, Autonomously developed, Independently deployable, Decentralized, Language–agnostic, Built and released with automated processes.