Hello to all the readers. In this post I’m going to show you how simple it is to test in Java using JUnit in NetBeans. First of all, I would like to say that this tutorial is only going to cover what we need to do to start testing simple programs for the TC3045 class in ITESM Guadalajara, that’s why we I’m going to use JUnit 4. The objective is to show how simple it is to setup the programs and start coding right away. Similar tutorials might be around the web and I’m not an expert on this subject, so if you would like to take a deeper look I recommend you going to the documentation in the official site. Everything in this tutorial is just the steps I did to have everything running in my PC(Linux). I hope you find it useful.
Step 0: Install Java
If you don’t have Java installed all you need to do is to run this commands con your console to install the JavaDevelopmentKit(JDK) in Ubuntu.
sudo add-apt-repository ppa:webupd8team/java
sudo apt install oracle-java8-installer
java -version
javac -version
Thanks to this tutorial for the help. If you are in other OS like Windows I recommend you to do the installation as any normal application, try the official java site for the JDK. Remember that sometimes you have to configure the PATH manually.
Step 1: Install NetBeans
- Go to the official site of NetBeans and download the newest version.

As NetBeans 10 does not have an installer we must do it manually with the binary files. Download the zip file and decompress it.


- Do the manual install for the bin files
For this I need you to open the terminal and go to the location of your netbeans folder. In my case I moved it to Desktop.

From here to do the installation insert the following commands:
sudo mv netbeans/ /opt
sudo ln -s /opt/netbeans/bin/netbeans netbeans
The first command moved the bin folder to /opt and the second command created a new command «netbeans» for the console that executes the program.
Now to open netbeans just write netbeans in your console.

Step 2: Create a project and write a java class
Lets star creating a new project. To avoid complications I like to create a project for the whole class and every file and exercise will be in this project.
- Click on the «New Project» button

- Select Maven and click next. We want a Java Application in Maven because it’s easier to handle dependencies.

- Write the project name and choose the location. Then click «Finish».

Now it’s time to create a class and start coding.
- Click on the «New File» button and create a new Java Class.

- Choose the class name and click finish. I’m going to call it Operator because the class will have functions to do simple operations.

Now we should have the class in out screen. I’m going to start adding simple function for sum, multiplication and a hello world.
public class Operator {
public int sum(int a, int b){
return a+b;
}
public int multiplication(int a, int b){
return a*b;
}
public String helloWorld(){
return "Hello World";
}
}
Step 3: Create a test for that specific class
NetBeans creates automatically a testing template for the class you want, all you have to do is to modify the tests or create more.
- Click on Tools and Create/Update Tests(You have to be on the Operator class)

**Ignore the typo in the picture 🙂
- Deselect the options on the image(we are not going to need that), and click OK. You can change the name of the new class if you want.

Now as you can see in the new test file all the functions from Operator have a testing function. If we run the tests now it will fail because we have to remove the fail() call and modify the test. The «assertEquals» at the end is what defines if the test passes, if the two paramenters are equal the test will pass. There are more options to create more specific tests but as I’m covering just the basics I’ll leave the code here with simple assertions that should pass.
public class OperatorTest {
/**
* Test of sum method, of class Operator.
*/
@Test
public void testSum() {
System.out.println("sum");
int a = 2;
int b = 3;
Operator instance = new Operator();
int result = instance.sum(a, b);
assertEquals(result, 5);
}
/**
* Test of multiplication method, of class Operator.
*/
@Test
public void testMultiplication() {
System.out.println("multiplication");
int a = 5;
int b = 3;
Operator instance = new Operator();
int result = instance.multiplication(a, b);
assertEquals(result, 15);
}
/**
* Test of helloWorld method, of class Operator.
*/
@Test
public void testHelloWorld() {
System.out.println("helloWorld");
Operator instance = new Operator();
String result = instance.helloWorld();
assertEquals(result, "Hello World");
}
}
To run the test all you have to do is right-click on the code editor and click on «Test File» or press the buttons Ctrl+F6. The output should be something like this:

For the purpose of the class, this configurations is still not enough because NetBeans 10 uses by default JUnit5 and the book of our lecture focuses on JUnit4. The final step is to install JUnit4. Remember that I said that we are going to use Maven because is easier to handle dependencies? This is the reason, with only a few steps more everything will be ready to go.
- Add the JUnit4 dependency.
For this you have to find the file pom.xml and open it.

Now inside the <dependencies> between any dependency add the next chunk of code:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

Save the file and then wait for the automatic installation.
- Change the import of the tests.
Check the imports of your test file, it’s using the library for JUnit5. All is left to do is to use the imports from JUnit4. Remove the import lines and copy paste this ones:
import static org.junit.Assert.assertEquals;
import org.junit.Test;

Now it’s all done!!
You are ready to go try your own classes and tests. If you want to learn useful tricks to do the activities check out this file, where you will learn to handle exceptions in testing. Remember that sometimes it is expected that the code fails, this tests are useful to test edge cases and avoid weird inputs.
If you want to see the sample code in GitHub here is the link to our project repository. It’s on a separate branch because is not related to the main testing, but it’s good to have samples somewhere in case we need them.
I hope this tutorial was helpful to you. Feel free to share this content or any content referenced here to help others as well. Keep learning and have fun.