Home > Java > javaTutorial > body text

Java Unit Testing

王林
Release: 2024-08-30 15:54:57
Original
1052 people have browsed it

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

What is Java Unit Testing?

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.

Basic Process of Java Unit Testing

  • To perform Java Unit Testing, the user needs to create test cases. Unit test cases ensure program logic works as it is expected.
  • junit is a package that contains interfaces and classes for junit testing.
  • Junit is a method in class used only for testing, i.e., known as Test Class. To mark the method as a Test method, it is annotated with @Test annotation, which executes code under test.
  • Users can also use Assert method or any other assert framework provided by Junit that helps to check the expected result with the actual result. These are called as asserts or assert statements.
  • Generally, unit test cases are created in separate source folders to keep test codes separate from real source codes. Standard conventions from Gradle and Maven build tools to use are:

src/main/java – For Java Classes

src/test/java – For Test Classes

  • Static imports are a feature of Java that allows methods and fields defined in the class as “public static” allowing to write shorter statements.

How to Create and Test Java Code with Junit Testing?

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:

Java Unit Testing

Give your project name and select JRE accordingly.

Java Unit Testing

Java Unit Testing

Step 3: Left side pane shows the java file. Then right-click on the project, and select New – Class

Java Unit Testing

Enter the class name and click on finish.

Java Unit Testing

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;
}
}
Copy after login

Output:

Java Unit Testing

Step 5: Now click on File – Junit Test case.

Java Unit Testing

Select Junit Test 4 and select setup(), then click on Finish.

Java Unit Testing

Click on OK.

Java Unit Testing

Below test code gets generated.

Java Unit Testing

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());
}
}
Copy after login

Output:

Java Unit Testing

Step 7: Right-click on the Test file, and select Run as – Junit test case.

Java Unit Testing

The left pane shows in green if the condition satisfies, i.e., functionality has been unit tested.

Java Unit Testing Framework and its Applications

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:

  • Time Efficient: Time saved by formulating guided and structured test cases backed up by rules makes sure quality management is way much better.
  • Cost Effective: Java Unit testing framework help in project cost management, reducing the waste or the expenditure that is caused by the allocation of resources or the shortcomings that might arise due to lack of management quality.
  • Maximized Automation: The usage of testing frameworks helps to decrease human resource involvement in a testing environment, further automating the process in an efficient manner.

Listing out the most popular java testing frameworks with their applications.

1. Junit

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.

2. Selenium

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.

3. SerenityBDD

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.

4. TestNG

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.

5. Mockito

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.

Conclusion

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!