XenonStack Recommends

Big Data Engineering

Test Driven Development for Java using JUnit | Quick Guide

Navdeep Singh Gill | 26 May 2023

Introduction

Before we get started, let us understand some basics. 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. Additionally we need software testing to ensure that the software fulfils the client specifications, responds correctly to inputs (input validation), runs in a reasonable time (performance testing), is easy to install and run (deployment testing), and meets the stakeholders' goals.

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


A key practice for extreme programming; it suggests that the code is developed or changed exclusively by the unit testing. Click to explore about, Test-Driven Development (TDD) Tools

What is 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 -
  1. Firstly, add a test.
  2. Run all the tests and see if any new test fails.
  3. Update the code to make it pass the new tests.
  4. Run the test again and if they fail then refactor again and repeat.

What is Behaviour Driven Development (BDD)?

It is similar to Test Driven Development. In other words, BDD is the extended version of TDD. The process is similar to it. 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 TDD. This type of development -
  • Explained the behaviour of the software/program.
  • User friendly
The major benefit of it is that it can easily be understood by a non-technical person also.
The key practice for extreme programming, suggests that the code is developed or changed exclusively on the basis of the unit test results. Source - Test Driven and Behavior Driven Development

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 bad code. It is necessary to write code with tests for best productivity and easy modifications.

What are the advantages?

  1. It gives a way to think through our requirements or design before we write our functional code.
  2. Enables us to take small steps during building software.
  3. Makes us 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 TDD is -
  • The most efficient and attractive way to proceed in smaller and smaller steps.
  • Following TDD means -
    • Fewer bugs
    • Higher quality software
    • Focus on single functionality at a given point in time

Why is Test Driven Development Important?

  • 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, it 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.

API Testing in TDD

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.
  • End-to-end functionality (how an API technically works).
  • Response time.
  • Load Testing
  • 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 -
  1. The output can contain status.
  2. The output can contain any type of data or information.
  3. 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 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.

Unit testing operates on a low level and is tightly coupled with source code. Click to explore about, Golang Unit Testing and Testing Best Practices

What are the best Test-Driven Development Tools for java?

 

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 -
  1. Web - HTTP, HTTPS (Java, NodeJS, PHP, ASP.NET, …)
  2. SOAP/REST Webservices
  3. FTP
  4. Database via JDBC
  5. LDAP
  6. Message-oriented middleware (MOM) via JMS
  7. Mail - SMTP(S), POP3(S) and IMAP(S)
  8. Native commands or shell scripts
  9. TCP
  10. 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. It allows programmers to create and test double objects (mock objects) in automated unit tests for the purpose of TDD. In simple words, we can say that Mockito is a framework that we specifically use to efficiently write certain kind of tests.

How to set up environment for Test Driven Development in Java?

The steps to set up environment for TDD in Java are listed below:

Setting up it 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 it 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.

Database Unit testing can be performed in a web application or desktop because the database can be used in the application. Click to explore about, Database Unit Testing and Test-Driven Database Development

How to implementing Java Test Driven Development?

The steps to implement Java TDD are described below:

Naming the test class

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 for production code

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. 3. 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 - we need to add an HTTP Request Sampler inside a thread group
  • 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 - enter the Server Name or IP of the API and its port (if required) in the web server section.
  • 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 - add HTTP Header Manager as a child to the HTTP Request sampler
  • At last, we add different listeners like 'Summary Report' to conclude.
Summary Report - Unit Testing With JMeter Then finally run the test. Unit Testing With JMeter - run the test In the 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);
 }
}

Conclusion

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 XenonStack Help You?

We follow the Test Driven Development Approach in the development of Enterprise-level Applications following Agile Scrum Methodology.

Application Modernization

Re-platforming, re-hosting, recoding, rearchitecting, re-engineering, interoperability,  of the legacy Software application for current business needs is called Application Modernization. It 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 the 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.