{{announcement.body}}
{{announcement.title}}

Java Testing Complete Guide

DZone 's Guide to

Java Testing Complete Guide

In this article, we discuss nine of the most common types of application testing and review code samples and tools for each in Java.

· Java Zone ·
Free Resource

The best way to write a good application is to cover it with the right tests. In this article, I describe the majority of existing testing types. All examples will be implemented with Java.

Unit Testing

Unit testing verifies that each application function (module) returns the correct value for a given input. By counting all lines executed during the test, you can find your code coverage. Code coverage is still one of the most important code quality metrics.

Possible Java unit testing frameworks: JUnit, TestNG

Test example:

Unit testing example

Unit testing example

Mutation Testing

Mutation testing is a way to find a missing tests in your code. The main idea of mutation testing says: once all unit test are written, every change in your code should fail at least of the existent unit tests. Otherwise, you have one missing test.

Java mutation frameworks: Pitest. More details about PITEST with examples. 

Test example:

Mutation test example

Mutation test example

Integration Testing

The main purpose of integration testing is to check the compatibility and consistency of a group of modules or a whole application. Depending on the application container, different frameworks are used. 

You may also like: Automated Testing: Improving Application Speed and Quality.

Java integration frameworks (container-independent): Mockito, PowerMock.

 Test example:

Integration test

Integration test example

UI Testing

Most of the application has some user interface. It might be web page or desktop frame. Java frameworks provide both (eg: javafx and vaadin).  User interface testing often plays a role of end to end testing. I think UI testing is the most effective in terms of time and quality assurance.

Java UI testing for web: selenium for desktop: Winium

Test example:

UI Testing

UI Testing example

Protocol Testing

Java support hundreds of different protols: TCP/IP, HTTP, SOAP, UDP, FTP, SMTP, etc. In this example, I'll use HTTP, as it's the most popular protocol.

Java HTTP tests frameworks: spring.

Server test example:

Protocol testing example

Protocol testing example

Benchmark Testing

The best way to precisely measure your code's efficiency is to use benchmark frameworks. Benchmark test give you very fair code execution results, ignoring possible side effects (e.g. warm-up stages). 

Java benchmark framework: JMH. More details in this article.

Code example: 

Benchmark testing example

Benchmark testing example

Performance Testing

To find out what load your application can withstand, you need to use performance testing tools (also known as stress testing tools). Performance measures depend on the protocol used, but there are already tools that support most popular protocols.

Performance application (might be integrated with Java): Jmeter.

Test example:

Performance testing example

Performance testing example

Security Testing

To find out how vulnerable an app is, you can use security test solutions. Security testing also depends on protocols of your application. But, often, security tool cover the most known use cases, like SQL injection or XSS injection.

Security tool: Arachni Scanner.

Script example:

Vulnerability testing example

Vulnerability testing example

Database Testing

In most cases, database are a large part of an application. A database can be tested in many ways: schema check, security check, performance, etc. 

Java database test engine: DbUnit.

Test example:

Database testing example

Database testing example

New ways to test applications are growing every day and so are the number of technologies used to perform tests, but this list of nine types of testing should be useful for quite long time. 


Further Reading

Topics:
benchmarking ,database testing ,http testing ,integration testing ,mutation testing ,performance testing ,security testing ,test ,ui testing ,unit testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}