Unit Testing with JUnit
NetBeans as a tool for performing software testing in Java. NetBeans provides
built-in support for JUnit, making it easy to write, run, and manage test cases.
Additionally, you can integrate Selenium for automated testing, JaCoCo for code
coverage, and even configure it for continuous integration (CI/CD).
1. Unit Testing with JUnit in NetBeans
NetBeans has built-in support for JUnit.
Steps:
1. Right-click on the Java class file.
2. Select Tools > Create Tests.
3. Choose JUnit 5 (or JUnit 4).
4. Implement test methods and assertions.
5. Run tests using Run > Test File.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/[Link] to edit this
template
*/
package midlabq2;
import [Link].*;
/**
*
* @author moizk
*/
public class Midlabq2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
char[][][] array = {{{' ', '', ' ', ' ', ' ', ' ', ' ', ' ', '', ' '},
{' ', '', '', ' ', ' ', ' ', ' ', ' ', '*', ' '},
{'#', '', ' ', '', ' ', ' ', ' ', '', '', '#'},
{' ', '', ' ', ' ', '', ' ', '', ' ', '', ' '},
{' ', '', ' ', ' ', ' ', '', ' ', ' ', '*', ' '},
{'#', '', ' ', ' ', ' ', ' ', ' ', ' ', '', '#'}}};
int i = 0;
do{
do{
int j= 0;
do{
[Link](array[0][i][j]);
j++;
}while(j<array[0][i].length);
[Link]();
i++;
}while(i<array[0].length);
}while(false);
}
}
---------------------------------------------------------------------------------------------------------
Main Java Class
package [Link];
public class Calculator {
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
// Method to subtract two numbers
public int subtract(int a, int b) {
return a - b;
}
// Main method to manually test the methods
public static void main(String[] args) {
Calculator calculator = new Calculator();
// Testing addition
int sum = [Link](5, 3);
[Link]("5 + 3 = " + sum); // Expected output: 8
// Testing subtraction
int difference = [Link](10, 4);
[Link]("10 - 4 = " + difference); // Expected output: 6
}
}
=====================================
Other Java Calculator Class
package softtst;
/**
*
* @author Usman Ali
*/
public class Calculator {
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
// Method to subtract two numbers
public int subtract(int a, int b) {
return a - b;
}
}
==================================
Junit Test Class
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/UnitTests/[Link]
to edit this template
*/
package softtst;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import static [Link].*;
/**
*
* @author Usman Ali
*/
public class CalculatorTest {
private Calculator calculator; // Declare Calculator instance
public CalculatorTest() {
}
@Before
public void setUp() {
[Link]("Running @Before - Executes before each test case");
calculator = new Calculator(); // Initialize calculator before each test
}
@After
public void tearDown() {
[Link]("Running @After - Executes after each test case");
}
/**
* Test of add method, of class Calculator.
*/
@Test
public void testAdd() {
[Link]("Testing add method");
int a = 5;
int b = 3;
int expResult = 8;
int result = [Link](a, b);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
}
/**
* Test of subtract method, of class Calculator.
*/
@Test
public void testSubtract() {
[Link]("Testing subtract method");
int a = 10;
int b = 4;
int expResult = 6;
int result = [Link](a, b);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
import [Link].*;
import [Link].*;
public class ChairArrangementGUI {
public static void main(String[] args) {
// Students array
String[] students = {"Ali", "Ahmed", "Sara", "Zara", "Umar", "Tariq", "Hina",
"Faizan"};
// 2D Array to store 8 arrangements (8 rows, 7 columns)
String[][] arrangements = new String[8][7];
// Fill the arrangements using nested while loop
int i = 0;
while (i < 8) {
int j = 0, k = 0;
while (j < 8) {
if (j != i) {
arrangements[i][k] = students[j];
k++;
}
j++;
}
i++;
}
// Create the GUI
JFrame frame = new JFrame("Chair Arrangements");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](500, 400);
JTextArea textArea = new JTextArea();
[Link](false);
[Link](new Font("Monospaced", [Link], 14));
// Show arrangements in text area
for (int row = 0; row < [Link]; row++) {
[Link]("Arrangement " + (row + 1) + " (Standing: " +
students[row] + "):\n");
for (int col = 0; col < arrangements[row].length; col++) {
[Link]("Chair " + (col + 1) + ": " + arrangements[row][col]
+ "\n");
}
[Link]("\n");
}
[Link](new JScrollPane(textArea));
[Link](true);
}
}