Creating unit tests

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.

Getting ready

For this recipe we will need a Java class with testable methods.

To start with JUnit we will need a Java project:

  1. Create a new project, this can be achieved by either clicking File and then New Project or pressing Ctrl+Shift+N.
  2. On New Project window on categories side choose Java and on Projects side select Java Application and click Next >.
  3. Under Name and Location: Name the project as JUnitApp.
  4. De-select the Create Main Class option.
  5. Click Finish.

After project creation we will add a Java class that will be tested.

  1. Right-click the default package of our JUnitApp.
  2. Select New and Java Class....
  3. Name it as Person and click Finish.

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:

  1. Right-click the body of Person.java then select Refactor... and Encapsulate Fields....
  2. Click on Select All button and then the Refactor button.

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.

How to do it...

Now let's start creating JUnit tests:

  1. Right-click on the Project under JUnitApp project and select New and Other.
  2. On New Project window on categories side choose JUnit and on File Type select Test for Existing Class and click Next >.
  3. Under Existing Class to Test, click on the Browse button, and on the Select Class dialog expand the default package, select Person.java and click OK.
  4. Leave all the default options marked and click Finish.

    New Test for Existing Class should look like the following screenshot:

    How to do it...
  5. Select JUnit 4.x and click Select.
How to do it...

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);

Save PersonTest.java and press Shift+F6 to run the class.

How it works...

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:

How it works...

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.

There's more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.233.94