Home » Core Java » junit » JUnit NetBeans Example

About Alvin Reyes

Alvin Reyes
Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.

JUnit NetBeans Example

1. Introduction

In this post I will introduce the basics of writing and running JUnit unit tests in NetBeans IDE. Testing an application is an integral part of the development cycle, and writing and maintaining unit tests can help ensure that the individual methods in your source code work correctly. The IDE’s support for JUnit testing framework allows developers to quickly create JUnit tests cases.

As of this writing, the latest NetBeans beta version is 8.1, using Java 7 version or above.
 
 

2. Set it up

The NetBeans IDE is pretty much straightforward to use. You have tons of developer friendly options and shortcuts. My favourite feature is the plugins repository. There are a lot of useful plugins available and the repository keeps on growing. We won’t be dealing with that right now, so let’s just focus on creating a JUnit Test case.

2.1 New Java Project

Create a new project by clicking on File > New Project > Java > Java Application. This will bring up the screen as below.

Figure. 1.0 New Java Application Project

Figure. 1.0 New Java Application Project

This will be our project for this example. A complete source is available for download at the end of this post.

3. Source

3.1 Java Implementation source code

Here is our implementation source code. We introduced 3 methods that we can tests. 1st method is the preProcessing of the messages class that returns a new Envelope object, 2nd is just to get the consolidated list of messages, and 3rd to postProcess the messages. We use annotation based schematics in our test case using @Test to tag a method as a test case. See the implementation class below:

NetBeansSampleJUnitApplication.java

package netbeanssamplejunitapplication;

import com.areyes1.jgc.nb.junit.Envelope;
import com.areyes1.jgc.nb.junit.Message;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author alvinreyes
 */
public class NetBeansSampleJUnitApplication {

    public Envelope preProcessMessages(List listOfMessage) {
        
        //  Create new message and put it in the envelop
        Envelope env = new Envelope();
        env.setEnvelopeId("1");
        env.setMessage(new ArrayList());
        for(Message msg:listOfMessage) {
            env.getMessage().add(msg);
        }
        
        return env;
    }
    
    public List generateListOfMessages() { 
        
        Envelope env = new Envelope();
        env.setEnvelopeId("2LIST");
        env.setMessage(new ArrayList());
        env.getMessage().add(new Message("NAME1","BODY1","DESC1"));
        env.getMessage().add(new Message("NAME2","BODY2","DESC2"));
        
        return env.getMessage();
    }
    
    public Envelope postProcessMessages(List listOfMessage) {
                
        //  Create new message and put it in the envelop
        Envelope env = new Envelope();
        env.setEnvelopeId("2");
        env.setMessage(new ArrayList());
        for(Message msg:listOfMessage) {
            env.getMessage().add(msg);
        }        
        return env;
    }
    
    
}

3.2 JUnit Test case

We created the JUnit test case by right clicking on the class > Tools > Create/Update test cases. This will bring up the following screen which will allow the developer to specify the test package, test class names and methods that will be included.

Figure 2.0 Creating new JUnit test case in NetBeans

Figure 2.0 Creating new JUnit test case in NetBeans

The option above will generate the source code similar to the one below.

NetBeansSampleJUnitApplicationTest.java

package netbeanssamplejunitapplication;

import com.areyes1.jgc.nb.junit.Envelope;
import com.areyes1.jgc.nb.junit.Message;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author alvinreyes
 */
public class NetBeansSampleJUnitApplicationTest {
    
    public NetBeansSampleJUnitApplicationTest() {
    }
   
    /**
     * Test of preProcessMessages method, of class NetBeansSampleJUnitApplication.
     */
    @Test
    public void testPreProcessMessages() {
        System.out.println("preProcessMessages");
        List listOfMessage = new ArrayList();
        listOfMessage.add(new Message("NAME1","BODY1","DESC1"));
        listOfMessage.add(new Message("NAME2","BODY2","DESC2"));
        listOfMessage.add(new Message("NAME3","BODY3","DESC3"));
        NetBeansSampleJUnitApplication instance = new NetBeansSampleJUnitApplication();
        Envelope result = instance.preProcessMessages(listOfMessage);
        assertTrue(result.getMessage().size() > 0); //  there should be messages processed
       
    }

    /**
     * Test of generateListOfMessages method, of class NetBeansSampleJUnitApplication.
     */
    @Test
    public void testGenerateListOfMessages() {
        System.out.println("generateListOfMessages");
        NetBeansSampleJUnitApplication instance = new NetBeansSampleJUnitApplication();
        List result = instance.generateListOfMessages();
        assertTrue(result.size() > 0); //  there should be messages processed
    }

    /**
     * Test of postProcessMessages method, of class NetBeansSampleJUnitApplication.
     */
    @Test
    public void testPostProcessMessages() {
        System.out.println("postProcessMessages");
        List listOfMessage = new ArrayList();
        listOfMessage.add(new Message("NAME1","BODY1","DESC1"));
        listOfMessage.add(new Message("NAME2","BODY2","DESC2"));
        listOfMessage.add(new Message("NAME3","BODY3","DESC3"));
        NetBeansSampleJUnitApplication instance = new NetBeansSampleJUnitApplication();
        Envelope expResult = new Envelope();
        Envelope result = instance.postProcessMessages(listOfMessage);
        
        //  We are expecting that the composition of the class changed.
        assertFalse(expResult.equals(result));
    }
    
}

4. Running the code

Running the JUnit test class above will give us the following result in NetBeans.

Figure 3.0 JUnit Test case result in NetBeans

Figure 3.0 JUnit Test case result in NetBeans

5. Download the NetBeans project

This was an example of JUnit Test case in NetBeans.

Download
You can download the full source code of this example here: junit-netbeans-example.zip
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of