0% found this document useful (0 votes)
2 views8 pages

Software Testing Lab

The document provides a guide on unit testing in Java using JUnit within the NetBeans IDE, highlighting its built-in support for creating and managing test cases. It includes steps for setting up JUnit tests, along with examples of a Calculator class and its corresponding JUnit test class. Additionally, it features a Java program for chair arrangements using a graphical user interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Software Testing Lab

The document provides a guide on unit testing in Java using JUnit within the NetBeans IDE, highlighting its built-in support for creating and managing test cases. It includes steps for setting up JUnit tests, along with examples of a Calculator class and its corresponding JUnit test class. Additionally, it features a Java program for chair arrangements using a graphical user interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this
template
*/
package midlabq2;

import javax.swing.*;

/**
*
* @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{
System.out.print(array[0][i][j]);
j++;
}while(j<array[0][i].length);
System.out.println();
i++;
}while(i<array[0].length);
}while(false);
}

}
---------------------------------------------------------------------------------------------------------
Main Java Class
package com.example;

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 = calculator.add(5, 3);
System.out.println("5 + 3 = " + sum); // Expected output: 8

// Testing subtraction
int difference = calculator.subtract(10, 4);
System.out.println("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/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/UnitTests/JUnit4TestClass.java
to edit this template
*/
package softtst;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author Usman Ali
*/
public class CalculatorTest {
private Calculator calculator; // Declare Calculator instance
public CalculatorTest() {
}

@Before
public void setUp() {
System.out.println("Running @Before - Executes before each test case");
calculator = new Calculator(); // Initialize calculator before each test
}

@After
public void tearDown() {
System.out.println("Running @After - Executes after each test case");
}

/**
* Test of add method, of class Calculator.
*/
@Test
public void testAdd() {
System.out.println("Testing add method");
int a = 5;
int b = 3;
int expResult = 8;
int result = calculator.add(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() {
System.out.println("Testing subtract method");
int a = 10;
int b = 4;

int expResult = 6;
int result = calculator.subtract(a, b);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.

import javax.swing.*;
import java.awt.*;

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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);

JTextArea textArea = new JTextArea();


textArea.setEditable(false);
textArea.setFont(new Font("Monospaced", Font.PLAIN, 14));

// Show arrangements in text area


for (int row = 0; row < arrangements.length; row++) {
textArea.append("Arrangement " + (row + 1) + " (Standing: " +
students[row] + "):\n");
for (int col = 0; col < arrangements[row].length; col++) {
textArea.append("Chair " + (col + 1) + ": " + arrangements[row][col]
+ "\n");
}
textArea.append("\n");
}

frame.add(new JScrollPane(textArea));
frame.setVisible(true);
}
}

You might also like