In the development of large-scale systems using Java, more and more projects are adopting agile development methods instead of the conventional waterfall type. When adopting the method of writing test code and program code in parallel, which is often adopted in agile development, there is a test framework as a tool to proceed with testing efficiently.
This time, we will take up “JUnit”, “Checkstyle”, and “FindBugs” from Java test frameworks, and introduce the merits of using them and points to note when introducing them.
Java features and the importance of unit testing
Java is a suitable language for large-scale system development and application development. A program written in Java is executed by a Java virtual machine (JVM), so the same operation can be performed independently of the OS.
Java, an object-oriented language, treats data processing and collections as one thing (object). Developers only need to understand what can be done and what results can be obtained by using each thing, not understanding the processing and procedure of the program.
When developing a large-scale system with a large number of people, even if you do not understand all the internal structures and processes of the program, you can proceed with the implementation as long as you know the role of each thing.
In Java, which is based on the premise that processing and data collections are used as units and that they operate accurately to provide determining processing, it is the basis of quality assurance that the movement of programs in module and method units is guaranteed. Therefore, unit testing is important.
Unit tests are tests mainly performed by developers to verify that units such as functions and methods that are components of a program are operating correctly as intended. While unit testing makes it easier to identify and fix defects, large-scale systems often have a huge number of units and the burden of unit testing is heavy, so test execution and confirmation of results are automated. A test framework is used.
In the next chapter, we will introduce test frameworks and test tools called “JUnit”, “Checkstyle”, and “FindBugs” that are often used in Java coding and unit testing.
Unit test automation framework “JUnit”
JUnit is an open-source testing framework used in Java development. You can use the test methods provided by JUnit to compare test expectations with test results and visualize code coverage.
Benefits of JUnit
The created test code can be executed automatically, and if there is an error, the location of the error and what kind of error it is can be known, so the test load can be reduced. Since previously run tests can be run again, it is easy to automate and refactor regression tests (regression tests) to detect degradation.
Also, if you install Eclipse, which is one of Java’s integrated development environments, the JUnit plugin will be installed, so the hurdles for introducing JUnit will be relatively low.
Disadvantages of JUnit
JUnit has its disadvantages as well as its advantages. For example, each test in JUnit assumes independence, and if you continuously execute tests for functions and methods that are dependent on other units, it will take time to define the test execution order.
Also, testing the program’s exception handling to work properly complicates the test code and requires test authors to be accustomed to writing test code to some extent.
JUnit has the above-mentioned disadvantages, but basically, JUnit is a tool that can produce high effects if you carefully select the target to be used and continuously modify it, and the larger the development project, the more the advantage will be demonstrated. To do.
Static analysis tools “Checkstyle” and “FindBugs”
From here, we will introduce the features of both while comparing the static analysis tools “Checkstyle” and “FindBugs”.
Coding convention check tool “Checkstyle”
Checkstyle is a static analysis tool for checking coding conventions in source code. In addition to pointing out coding styles such as writing curly braces and blank spaces in system code, make sure that naming conventions such as class names, constants, and variables are written correctly according to the rules defined as coding conventions.
Compared to “FindBugs” which will be introduced later, it is a tool that is superior in checking coding styles and naming conventions.
[Characteristics of Checkstyle]
- Source code is analyzed
- Providing rules based on coding conventions
- Customizable rules to apply
- Custom rules can be added
It is possible to check various aspects of the source code. You can also find class design problems, method design problems, and even check for code layout and formatting problems.
“FindBugs” to check for potential bugs
FindBugs is a static analysis tool that can check and prevent potential bugs. You can check for enhanced code by extracting from your program the parts that fit a particular code pattern to indicate potential bugs. Since it points out the possibility of NullPointerException (one of the exceptions in the programming language Java) that uses method parameters without performing a null check, it is possible to prevent bugs from being introduced due to careless mistakes. ..
Compared to Checkstyle introduced above, it is a tool that is good at detecting potential bugs.
[Characteristics of FindBugs]
- Providing rules specific to potential bugs
- Bytecode is the analysis target
- You can select the rule to apply
- Difficult to extend rules
FindBugs can create custom rules using the bytecode scanning check feature. Since BCEL (Byte Code Engineering Library) is used to implement the checking function, even simple rule creation requires a lot of time and verification.
The two tools can be used efficiently by using them in combination. It is necessary to understand the characteristics of each tool in advance and customize it such as deleting unnecessary rules and creating project-specific rules, but what the tool can do is do as much as possible to the tool and not carry over the problem to the visual check review. , Leads to system development support.
To implement a test framework in the field
While deploying a test framework has many advantages, the burden of writing test code for deployment must be considered. In addition, it is a prerequisite for the introduction that the developer is familiar with the framework and that an organizational mechanism for continuing maintenance is created.
When introducing the framework, it is a good idea to first narrow down the tests you want to introduce and the target program and partially introduce it, and then gradually expand the target range while observing the situation. Also, some types of tests cannot be created with a specific test tool, so consider using them with other frameworks.
For example, JUnit does not allow you to test database connections or operations. In this case, you need to use another framework such as DBUnit.
We recommend choosing a popular framework to improve test quality and lower developer learning costs. It is important to understand the characteristics and specifications of each framework and accumulate know-how within the team.
This time, we introduced “Junit”, “Checkstyle”, and “FindBugs”, which are test frameworks that can be used for Java development. In particular, the use of frameworks is effective for improving the efficiency of unit tests in large-scale development. However, there is a concern that the burden on developers will increase due to the introduction of frameworks such as framework learning and test code creation, so in consideration of the development schedule and the scale of the project, step-by-step introduction, and multiple frames It is recommended to use the work together.
If you ever want to know about similar things, check out the Facebook page Maga Techs.