TDD Using JUnit And Mockito Unit Test PART1

Posted By : Ankur Bansala | 28-Dec-2018

  Overview:

In the life cycle of software development, Software testing plays an important role. It is used to identify errors and bugs during software development and also increase the quality of the product. There are many ways and approaches and Test Driven Development approach is one of them. For extreme programming Test Driven Development is a key practice, it shows us that the code is developed or changed exclusively on the basis of the Unit Testing.

TDD (Test Driven Development):

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

  • Firstly, add a test.
  • First Run the tests and see if any new test fails.
  • Second Update the code to make it pass the new tests.
  • Third Run the test again and if they fail then refactor again and repeat.


Why Test Driven Development?
Advantages of Test Driven Development

Before we write our functional code It gives a way to think through our requirements or design. During building, software is a programming technique that enables us to take small steps. To code in large steps, It is more productive in nature rather as compared attempting. Let's take an example if you've written two new lines of code than a thousand and assume that you write some code and then compile it and then test it and maybe there are chances of failure. it becomes easy to fix those defects also easy to find.
Basically,
A Test Driven Development is - To proceed in smaller and smaller steps its the most efficient and attractive way.
Following Test Driven Development means -

  • Fewer Bugs.

  • Higher quality software.

  • At a given point in time, Focus on single functionality.

 


Need for Test Driven Development

Requirements - more focus on requirements in depth.

 

  • Rapid Feedback - multiple small changes Vs One large change.

  • Values Refactoring - for lower impact and risk, Refactor the code.

  • Design to Test - good design practice would be TDD Approach.

  • Tests as information -assumptions and Documenting decisions


Test Driven Development with Java

Test Driven Development plays an important role In the Java community and in implementation and of a software/web app. TDD helps the programmer in several ways, such as - Improving the code. Increasing the programmer’s productivity Side by side. It Will saves our time no wasted for rework. Able to identify the error and faster. Instead of writing the big classes the programmer will be able to write small classes which will be focused only on a single functionality. While programming, whenever the code becomes bigger it becomes very difficult to change and debug the code because there is a high chance of the code getting messed up its the major problem with programmers But if we are using Test Driven Development technique - Means we have automated tests. Its a safe side for the programmers if programmers are writing the test cases for their program. It becomes also easy to view the error, where it is and how it is paralyzing our code.

Test Driven Development Tools

There are many tools available for testing and improving the software system. Most common testing tools are listed below -

JUnit for Unit Tests

Its a framework of unit testing and designed for Java programming language. We can check the business logic of any class With the help of unit tests, . It is families of unit testing frameworks which is known as the xUnit that originated with SUnit.

Mockito for Rest API Testing

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

Unit Testing With JUnit
>

 

 

 



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


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);
 }
}


 

 



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

 

 



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 good to say that Test Driven Development should be used by as many developers as possible, to improve not only the code quality but also to increase the productivity with the development of software/program. Test Driven Development also leads to more modularized, flexible and extensible code.

About Author

Author Image
Ankur Bansala

Ankur is an experienced Java back-end Developer and having capabilities to build web application.

Request for Proposal

Name is required

Comment is required

Sending message..