Every program needs to be tested. There are many types of testings out there. Today we are going to talk about unit testing.
- Unit Testing.
The software consists of many units with specific logic. Unit testing is a type of testing that tests each unit separately to verify that individual parts work correctly. The unit can be represented as a module, class, and function. Most of the time unit testing is done at the functional level. Basically, we test our methods(functions) to work as expected in our source code.
Unit testing is a set of automated test cases. Usually, it’s in the same source project and created by the developers themselves.
Let’s see how we can do unit testing without using any ready solution libraries.
We would just use our method with different arguments and see if it’s returning the correct output. It’s not really automated every time we run this code we need to look at the output and see if it printed correctly.
We need something to validate the output and we need many other features to create robust automated test suites.
Make changes fearlessly. Set of unit tests lets us make changes without risking breaking existing-working functionalities(if a change breaks something, the test suite will catch it and we can apply the fix).
JUnit is one of the libraries for unit testing in Java.
- There is no main method. JUnit provides its own run engine.
- We have separate test cases. The test case in JUnit is represented by a void method tagged with @Test annotation.
- Most of the test cases will have input, actual results, expected results. Each test case should have assertions to validate the actual result with the expected result.
Let’s talk about where we should have our unit test suite.
We keep our unit test classes under the test folder.
By following Maven structure, the actual source code of the application goes:
And the testing code goes:
We will not go in-depth into JUnit. If you want to learn more please go to their official docs.
I would like to mention TDD in this article because it became a big part of software development. In TDD, we write our test cases first and then we implement the actual functionality.
This is a sequel of TDD flow based on the book Test-Driven Development by Example:
1. Add a test.
2. Run all tests. The new test should fail for expected reasons.
3. Write the simplest code that passes the new test.
4. All tests should now pass.
5. Refactor as needed, using tests after each refactor to ensure that functionality is preserved.
That’s all for this article. Happy codings!
The resources and links used in this article:
Unit testing - Wikipedia
In computer programming, unit testing is a software testing method by which individual units of source code-sets of one…
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET