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

Reading Time: 10 Minutes

by  | October 20, 2017  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 -

  • Firstly, add a test.

  • Run all the tests and see if any new test fails.

  • Update the code to make it pass the new tests.

  • Run the test again and if they fail then refactor again and repeat.

What is Test Driven Development?


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 -

  • Explained the behaviour of the software/program.

  • User friendly

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

  • It gives a way to think through our requirements or design before we write our functional code.

  • It is a programming technique that enables us to take small steps during building software.

  • It is more productive in nature rather as compared attempting to code in large steps.

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 -

  • The most efficient and attractive way to proceed in smaller and smaller steps.

  • Following Test Driven Development means -

    • Less bugs

    • Higher quality software

    • Focus on single functionality at a given point in time

Need for Test Driven Development

  • Requirements - Drive out requirement issues early (more focus on requirements in depth).

  • Rapid Feedback - Many small changes Vs One big change.

  • Values Refactoring - Refactor often to lower impact and risk.

  • Design to Test - Testing driving good design practice.

  • Tests as information - Documenting decisions and assumptions.

Why We Use 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 -

  • Improving the code

  • Side by side, increasing the programmer’s productivity.

Using Test Driven Development concept in our programming skills -

  • Will save our time which is getting wasted for rework.

  • Able to identify the error/problem quicker and faster.

  • The programmer will be able to write small classes which will be focused only on a single functionality instead of writing the big classes.

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 -

  • Means we have automated tests

  • We are writing the test cases for our program which is a safe side for the programmers.

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 -

  • Testing APIs (Application Programming Interfaces)

  • To determine if the API created meets the expectations.

  • Testing end-to-end functionality (how an API technically works).

  • Testing the response time.

  • Testing the load that the particular API is able to handle.

  • Only API functions are tested.

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 -

  • The output can contain the status.

  • The output can contain any type of data or information.

  • Or an API can be calling another API.

API Testing can be done by -

  • Unit testing (testing the API methods and checking if the output meets the expectations).

  • Using many tools for checking the performance and load i.e that how many requests can be handled by the API at the same time and many more. We can use Apache JMeter for performance and load testing.


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 For Unit Testing

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

JMeter For Load and 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 -

  • Web - HTTP, HTTPS (Java, NodeJS, PHP, ASP.NET, …)

  • SOAP/REST Webservices

  • FTP

  • Database via JDBC

  • LDAP

  • Message-oriented middleware (MOM) via JMS

  • Mail - SMTP(S), POP3(S) and IMAP(S)

  • Native commands or shell scripts

  • TCP

  • Java Objects

Mockito for Rest API Testing

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

  • Install Java first.

  • Then we need Eclipse or any other IDE(Integrated Development Environment).

  • Create a new project (Maven project) and place the JUnit maven dependency in pom.xml file.

OR

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

Setting Up TDD with JMeter

  • Install Java.

  • Install Apache JMeter

    • For Ubuntu - $ sudo apt-get install jmeter

    • For CentOS7 - sudo yum install jmeter

Setting Up TDD with Mockito

  • Install Java.

  • Then we need Eclipse or any other IDE(Integrated Development Environment).

  • Create a new project and download the mockito jar and include it in the project and if it is a maven project then add mockito dependency in pom.xml file for that particular project.


Implementing Test Driven Development

  • Naming - We use common conventions in naming the test class. Let’s start with the name of the class which is being tested and assume the name of that class is “Student”. In that case, the name of the test class should be “StudentTest”. We have to append “Test” to it. The Same naming convention is used in the case of methods. If there is a method “DisplayStudentAddress()”, then the name of the method in testing should be “testDisplayStudentAddress()”.

    Naming in Production

    Naming in Testing

    Student

    StudentTest

    DisplayStudentAddress()

    testDisplayStudentAddress()

  • Packages - We don’t use the same package for production code and testing code. The best practice is to use different source directories, “src/main/java” for production and “src/test/java” for testing.

  • Structure and Annotation - @Test annotation (JUnit4/5) tells JUnit to execute the testDisplayStudentAddress() method as a test method and report whether it passes or fails. As long as all assertions (if any) pass and no exceptions are thrown, the test is considered to pass. So our test code should follow AAA (Arrange Act Assert) pattern.

    • Create student object that will be tested (arrange) - Student student = new Student("Joe", "Brian");

    • Exercise the production code’s Student.getdisplayStudentName() method(act) - String displayName = student.getdisplayStudentName();

    • Verify if the result is as expected (assert) - assertEquals("Joe, Brian", displayName); If the result did not match, the test would be marked as failed.

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 JUnit

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 -

  • First of all, we need to add an HTTP Request Sampler inside a thread group (Test Plan -> Thread Group -> Sampler->HTTP Request)

Unit Testing With JMeter

  • Next, we need to enter the Server Name or IP of the API and its port (if required) in the web server section.

Unit Testing With JMeter

  • The user also needs to add HTTP Header Manager as a child to the HTTP Request sampler (for the current example this is not required) with the content-type header.

Unit Testing With JMeter

  • At last, we add different listeners like 'Summary Report' to conclude.

Unit Testing With JMeter

Then finally run the test.

Unit Testing With JMeter

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

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. 

  • Java Cloud Services

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

  • Continuous Integration and Continuous Deployment

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.

  • Microservices Architecture

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.