Software development process that includes requirements specification, analysis, design, implementation, testing, deployment, and maintenance. Testing is an important part of this process. This medium article introduces how to test Java classes using JUnit.
JUnit is a tool for testing Java programs.
JUnit is the de facto framework for testing Java programs. JUnit is a third-party open-source library packed in a jar file. The jar file contains a tool called test runner, which is used to run test programs. Suppose you have a class named A . To test this class, you write a test class named ATest . This test class, called a test runner, contains the methods you write for testing class A . The test runner executes ATest to generate a test report, as shown in Figure below.
To test a class, you need to write a test class and run it through JUnit to generate a report for the class.
To use JUnit, create a test class. By convention, if the class to be tested is named A , the test class should be named ATest . A simple template of a test class may look like this:
When this command is executed, JUnitCore controls the execution of ATest . It first executes the setUp() method to set up the common objects used for the test, and then executes test methods m1 and m2 in this order. You may define multiple test methods if desirable.
The following methods can be used to implement a test method:
- assertTrue(booleanExpression )
The method reports success if the booleanExpression evaluates true.
- assertEquals(Object, Object)
The method reports success if the two objects are the same using the equals method.
The method reports success if the object reference passed is null .
The method causes the test to fail and prints out the string.
Here is an example of a test class for testing java.util.ArrayList.
You can define any number of test methods. In this example, the two test methods testInsertion and testDeletion are defined. JUnit executes testInsertion and testDeletion in this order.
Here is an example of a test class for testing Loan custom class.
The testPaymentMethods() in LoanTest creates an instance of Loan (line 16–17) and tests whether loan.getMonthlyPayment() returns the same value as getMonthlyPayment(annualInterestRate, numberOfYears, loanAmount). The latter method is defined in the LoanTest class (lines 27–33).
The testPaymentMethods() also tests whether loan.getTotalPayment() returns
the same value as getTotalPayment(annualInterestRate, numberOfYears, loanAmount). The latter method is defined in the LoanTest class (lines 36–40).
The Loan class is shown in below.
Here is our checkpoint, we learnt them so far:
- What is JUnit?
- What is a JUnit test runner?
- What is a test class? How do you create a test class?
- How do you use the assertTrue method?
- How do you use the assertEquals method?
Now, we can continue to learn our last key point!
Using JUnit from Eclipse
JUnit is intergrated with Eclipse. Using Eclipse, the test program can be automatically generated and the test process can be automated.
This section introduces using JUnit from Eclipse.
Create a project named JUnitTesting as follows:
- Step 1: Choose File, New Java Project to display the New Java Project dialog box, as shown in Figure 2.
- Step 2: Enter JUnitTesting in the project name field and click Finish to create the project.
To demonstrate how to create a test class, we first create a class to be tested. Let the class be Loan. Here are the steps to create the Loan class under JUnitTesting project.
- Step 1: Right-click the project node JUnitTesting and choose New, Class to display the New Java Class dialog box, as shown in Figure 3below.
- Step 2: Enter mytest in the Package field and click Finish to create the class.
- Step 3: Copy the code Loan class and make sure the first line is package mytest , as shown in Figure 4.
Now you can create a test class to test the Loan class as follows:
- Step 1: Right-click Loan.java in the project to display a context menu and choose New, JUnit Test Case to display the New JUnit Test Case dialog box, as shown in Figure 5.
- Step 2: Click Finish. You will see a dialog prompting you to add JUnit 4 to the project build path. Click OK to add it. Now a test class named LoanTest is created as shown in Figure 6. You can now modify LoanTest by copying the code and run LoanTest.java.
You will see the test report as shown in Figure 7.
You can now modify LoanTest by copying the code from above. Run LoanTest.java. You will see the test report as shown in Figure 7.
Here is our summary that we learnt today:
- JUnit is an open-source framework for testing Java programs.
- To test a Java class, you create a test class for the class to be tested and use JUnit’s test runner to execute the test class to generate a test report.
- You can create and run a test class from Eclipse.
Testing is a quite important part of software development process process. We learnt the basics and sample developments about it using JUnit in this post!