NetBeans can easily automate the task of creating skeleton unit tests for our classes.
In this process the IDE can write an entire unit class based on the available methods of some code. This class can be given different configurations with just a single click.
NetBeans integrates by default two different versions of JUnit by default. It is up to the developer to choose the most familiar one.
The biggest difference between versions is that JUnit 4 relies heavily on features introduced by Java 5, such as generics and annotations. Be aware that to use JUnit 4 the minimum JDK requirement is version 5.
For this recipe we will need a Java class with testable methods.
To start with JUnit we will need a Java project:
After project creation we will add a Java class that will be tested.
We will then proceed to add fields in Person.java
. To do this, replace the content of it with the following code:
public class Person { String name; int age; }
Then let's encapsulate the fields and create a greet()method:
Person.java
then select Refactor... and Encapsulate Fields....Once the code has been refactored as the last method of the class, enter the following:
public String greet() { return "Hi, my name is " + name + "and I am " + age + ". Nice to meet you!"; }
Save the class.
Now let's start creating JUnit tests:
New Test for Existing Class should look like the following screenshot:
Now remove the results of the default behavior. In all of the methods remove the following:
// TODO review the generated test code and remove the default call to fail. fail("The test case is a prototype.");
In testGetName()
replace:
String expResult = "";
with:
String expResult = "John"; instance.setName(expResult);
In testSetName()
replace:
String name = "";
with:
String name = "John";
In testGetAge()
replace:
int expResult = 0;
with:
int expResult = 30; instance.setAge(expResult);
In testGreet(),
replace the whole body of the method with the following:
System.out.println("greet"); Person instance = new Person(); String name = "John"; instance.setName(name); int age = 30; instance.setAge(age); String expResult = "Hi, my name is " + name + "and I am " + age + ". Nice to meet you!"; String result = instance.greet(); assertEquals(expResult, result);
NetBeans creates the PersonTest under the Test Packages view of Projects window.
The IDE also adds the under Test Libraries the JUnit jar library.
If executing the tests once they were created will fail since they were all marked with the following:
// TODO review the generated test code and remove the default call to fail. fail("The test case is a prototype.");
But since we have removed the above line and inserted our code, we will see something like this when tests finish their execution:
On the previous screenshot we can see on the left-hand side the name of the executed methods and their status, all marked with green for success, and on the right-hand side the output created by the System.out.println()
calls within the named methods.
When the project grows in size and the unit classes spread like wildfire it becomes more and more cumbersome to navigate through the endless maze of packages, and can take some time to find the respective class (or test class for that matter) which corresponds to the one we are working with.
NetBeans developers have therefore come up with a useful shortcut that improves the handling of those classes.
Suppose Person.java
is open and we wish to open its JUnit-generated test class. Simply press Ctrl+Shift+T and the NetBeans will open PersonTest.java
for you. Alternatively it is also possible to navigate back to the other class, say you wish to quickly modify something during the test phase, pressing the shortcut again will bring you back to Person.java
.
3.139.233.94