A Hands-on introduction

Java is one of the most popular and widely used programming languages nowadays. Its popularity has increased thanks to its favourable features. To start with, it is beginner friendly and has an easy syntax and readability. In addition, it has a huge community and has impacted positively programmers’ career prospects. On another hand, it is a cross-platform language, as it runs on a virtual machine and can therefore operate on different operating systems.

As you may guess, Java didn’t hesitate on adding a pinch of other useful features to increase its greatness. It is implementing a lot of saving time libraries and frameworks. Amongst them are automated testing frameworks.

Automated testing

Testing frameworks are made for automated testing, which basically tests your code using additional data and procedures, to assure that every component works correctly (The code under test is called test coverage). Automated testing is included as a piece of code, by the developer, and provides early problem detection, which can be very important to keep a faster move on software development life cycle, as it spares us from detecting bugs line by line. One extremely interesting point is that it pushes developers to structure their code in the best possible way, since it may not be possible to test, if it is badly written.

To bring up the rear, if you intend to be a good developer and would like to start with the best practices, you should definitely learn automated testing.

Note that unit tests target small and simple code (eg. a method or a class). For complex component interaction testing, other types of testing may come into play.

Java’s best known testing frameworks

Now that you know what Java and automated testing are, you may want to dive into some of the most used frameworks, and maybe make your mind to pick up the one that best suits your needs and use it to its full potential. So, let’s get started!

Before defining each framework apart, I want you to know that each one uses its own annotations and methods. To give you a small idea about them, I am going to give examples for the JUnit framework.

  • JUnit is maybe the most popular one out there. It is used widely by Java developers, it is simple to use and you can run your test and get instant results from a green/red progress bar. JUnit is open source and you can download it as a JAR file from GitHub and add it to your test classpath. This framework is known for having a hand in the test-driven development process. A JUnit test is preceded by the @Test tag and uses assert methods to compare the expected and the actual results, and by which you can provide error messages to help every user of your code identify and fix the problems.
    The following code shows how a JUnit5 test can be integrated:

    import static org.junit.jupiter.api.Assertions.assertEquals;
    import org.junit.jupiter.api.Test;
    
    public class TestClass {
    
        @Test
        public void correctSubtraction() { // Test method
            Calculator calculator = new Calculator(); // Tested class
            int result = calculator.sub(2,3);
            assertEquals(-1,result,"Correct result"); // Param1: expected result, param2: actual result, param3: assertion message 
        }
    }
    • Some JUnit annotations:
      @Test: Identifies a method as a test method.
      @Before:
       To say that a method should be executed before each test (to prepare/initialize data for example).
      @After:
       To say that a method should be executed after each test (to do a cleanup to save memory for example).
      @Test (timeout=50):
       The test fails if the method takes more than 50 milliseconds.
    • Assertion methods can be found on the JUnit5 documentation.
  • Mockito uses mocks to allow an isolation testing with no dependencies (if you need a mocking ability, you have to use a mocking framework in conjunction with your favourite testing framework). Just as JUnit, Mockito is a unit testing framework, it is open source and has a rich documentation.
  • Arquillian is an integration testing framework which allows easy functional and acceptance testing. It is a highly innovative and extendible platform for JVM and integrates with different other frameworks. One special point about it is that it allows you to run your tests in the run-time and run other tests simultaniously in your IDE.
  • TestNG is another framework which shares the same basic component architecture as JUnit, but has additional functionalities and supports different automated testing types. Many developers prefer it over JUnit as it has more annotations which are easy to understand (JUnit’s can be confusing sometimes), it is more flexible than JUnit and doesn’t have as many constraints and it is meant for high-level and complex testing and covers the entire JUnit functionalities
  • Robot framework is an acceptance testing framework which uses the keyword-driven testing approach. It has a simple syntax and a lot of libraries, tools and APIs. Since it is written in Python, it can also run on Cython (C) and Jython (Java), which means that it is a cross-platform framework.

Conclusion

I have presented some of the available Java testing frameworks, the list goes on and you may find or be interested by others. Remember, there’s no “best” framework. Each one has its pros and cons. The choice you make is highly linked to how you will use the framework and which factors you prioritize (ease of use, learning curve, speed, good documentation, regular updates, supported IDEs and so on).

Effective Unit Testing is written to show how to write good tests—tests that are concise and to the point, expressive, useful, and maintainable. Inspired by Roy Osherove’s bestselling The Art of Unit Testing, this book focuses on tools and practices specific to the Java world. It introduces you to emerging techniques like behavior-driven development and specification by example, and shows you how to add robust practices into your toolkit.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.