Java Unit Testing is basically testing the Java application at the time of production. It is a method of testing the source code for fitment of use in a production environment. Java Unit testing helps in testing individual components of each unit in the software application. Unit is an individual testable component of software and is tested at the development phase.
Java Unit testing is one of the important steps in software designing and implementation, not only improves the effectiveness and efficiency of code but also reduces regression in further development and maintenance. In this article, we shall see the basic process of Java Unit testing, how to create the framework, and a few examples of testing applications.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Java unit testing is a framework for JUnit, is one of the regression testing methods. It is an open-source framework used to read and write repeatable automation test cases. Java Unit testing isolates sections of code and verifies the correctness.
In STLC, SDLC, and V Model, Unit testing is the first level of testing the application before integration testing. Java Unit testing is a type of White Box testing which is executed by the developer itself. In the practical world, due to some issues or any other dependencies, Quality Assurance Engineers also do unit testing sometimes.
src/main/java – For Java Classes
src/test/java – For Test Classes
Given below shows how to create and test java code with junit testing:
Step 1: In the first place, to test Junit code, we need to have Java Logic in Eclipse or IntelliJ, or any other IDE. Download Eclipse as of now from https://www.eclipse.org/downloads/
Once downloaded, install it onto the system and launch it.
Step 2: Click on New – Java Project as below:
Give your project name and select JRE accordingly.
Step 3: Left side pane shows the java file. Then right-click on the project, and select New – Class
Enter the class name and click on finish.
Step 4: Complete the class file as below with the given code.
Code:
package junit_test; public class Sample { int x, y; void Math(int x, int y) { this.x = x; this.y = y; } public int sum() { return x + y; } }
Output:
Step 5: Now click on File – Junit Test case.
Select Junit Test 4 and select setup(), then click on Finish.
Click on OK.
Below test code gets generated.
Step 6: Write the below code, for testing the functionality.
Code:
package junit; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class SampleTest { Math math; @Before public void setUp() throws Exception { math = new Math(8, 2); } @Test public void testSum() { Assert.assertEquals(17, math.sum()); } }
Output:
Step 7: Right-click on the Test file, and select Run as – Junit test case.
The left pane shows in green if the condition satisfies, i.e., functionality has been unit tested.
Java Unit testing framework is a set of guidelines that are followed at the time of creating test scripts by using Java when performing quality testing of the software. These rules define the basic structure of test and strategy for testing lifecycle, such guidelines include scripting, test data information, repositories, test results, etc.
Below are the features of Java Unit Testing frameworks:
Listing out the most popular java testing frameworks with their applications.
As we have seen, this article is completely on the Junit testing framework, to describe in precise, Junit is an open-source testing framework that works on Java Virtual Machine. Junit focuses primarily on efficient execution and creation of unit testing for applications, and also establishes Test Engine API that helps to discover, and execute unit tests on the framework.
Junit supports various IDEs, like IntelliJ, Eclipse, VSCode, etc. It also provides a console launcher for launching the JUnit platform from the Java console.
Application: Junit is used by QA testers or developers to meet Junit and Regression testing requirements.
It is an open-source testing tool for browser automation. Helps in creating test scripts using Python, Java, etc. to write automated test cases for web applications. Selenium has a web driver, grid, etc that helps in creating automated test cases on various browsers.
Application: Selenium is mostly used in cases where developers need it for cross-browser testing and for browser automation.
It is also an open-source framework that helps to write high-quality acceptance and regression test cases. Requirements are defined in terms of user stories for acceptance and are hence automated using JBehave.
SerenityBDD helps to keep track of the project and the application that has been tested.
Application: QA testers or developers use SerenityBDD for Regression testing and for Automated Acceptance testing, behavioural driven development.
It is an easier version with a lot of functionalities including annotations and an arbitrary thread pool to run tests. The framework is versatile and designed to cover all categories like unit tests, functional tests, integration, and end-to-end tests.
Application: QA testers or developers opt for TestNG as it helps in Functional testing, Unit, and Integration testing along with end-to-end testing.
It helps to minimize irrelevant interactions using expect run verify libraries. It focuses on testing selected behavior by proving ready to use, simple, and less expensive frameworks.
Application: QA testers and developers use Mockito to create a duplicate interface that can be used to test dummy functionalities in unit testing.
With this, we shall conclude the topic “Java Unit Testing”. We have seen what is Java Unit Testing, and what is meant by Junit along with the creation of a Unit test case with an example class file in Java. Also have seen various Java Testing frameworks and their applications.
The above is the detailed content of Java Unit Testing. For more information, please follow other related articles on the PHP Chinese website!