You are on page 1of 19

07-02-2023

CREATING JUNIT TEST


CASES USING NETBEANS
Dr. Madhusmita Sahu
Assistant Professor
Department of Computer Science and Information Technology

TOPICS TO BE DISCUSSED

JUnit Path Setting


Creating Junit test
case
Prerequisites
1 3

2 4
07-02-2023

JUNIT

• JUnit is an open-source framework that is used for writing and executing unit tests in
Java programming language.
• It is one of the best-known unit testing frameworks.
• Erich Gamma and Kent Beck initially develop it.

JUNIT

• Different well-known automation unit testing tools are


07-02-2023

NEED OF JUNIT TESTING

•It finds bugs early in the code, which makes our code
more reliable.

•JUnit is useful for developers, who work in a test-driven


environment.

•Unit testing forces a developer to read code more than


writing.

•You develop more readable, reliable and bug-free code


which builds confidence during development.

FEATURES OF JUNIT

• JUnit is an open source framework, which is used for writing and running tests.
• Provides annotations to identify test methods.
• Provides assertions for testing expected results.
• Provides test runners for running tests.
• JUnit tests allow you to write codes faster, which increases quality.
07-02-2023

FEATURES OF JUNIT

• JUnit is elegantly simple. It is less complex and takes less time.


• JUnit tests can be run automatically and they check their own results and provide
immediate feedback. There's no need to manually comb through a report of test
results.
• JUnit tests can be organized into test suites containing test cases and even other test
suites.
• JUnit shows test progress in a bar that is green if the test is running smoothly, and it
turns red when a test fails.

WHAT IS A UNIT TEST CASE?

• A Unit Test Case is a part of code, which ensures that another part of code (method)
works as expected.
• To achieve the desired results quickly, a test framework is required.
• JUnit is a perfect unit test framework for Java programming language.
• A formal written unit test case is characterized by a known input and an expected
output, which is worked out before the test is executed.
07-02-2023

WHAT IS A UNIT TEST CASE?

• The known input should test a precondition and the expected output should test a
post-condition.
• There must be at least two unit test cases for each requirement −
• one positive test and
• one negative test.

• If a requirement has sub-requirements, each sub-requirement must have at least two


test cases as positive and negative.

ANNOTATIONS FOR JUNIT TESTING

• The Junit 4.x framework is annotation based, so let's see the annotations that can be
used while writing the test cases.

@Test • annotation specifies that method is the test method.


• annotation specifies that method will be failed if it takes longer
@Test(timeout=1000)
than 1000 milliseconds (1 second).
• annotation specifies that method will be invoked only once,
@BeforeClass
before starting all the tests.
• annotation specifies that method will be invoked before each
@Before
test.
@After • annotation specifies that method will be invoked after each test.
• annotation specifies that method will be invoked only once, after
@AfterClass
finishing all the tests.
07-02-2023

ASSERT CLASS

• The org.junit.Assert class provides methods to assert the program logic.

METHODS IN ASSERT CLASS

void assertEquals(boolean • checks that two primitives/objects are equal.


expected,boolean actual) • It is overloaded.

void assertTrue(boolean • checks that a condition is true.


condition)

void assertFalse(boolean • checks that a condition is false.


condition)

void assertNull(Object • checks that object is null.


obj)

void assertNotNull(Object • checks that object is not null.


obj)
07-02-2023

CREATING A JUNIT TEST CASE IN


NETBEANS

• Install Java Development Kit


1
(JDK)
2 • Install NetBeans IDE
3 • Download JUNIT

PATH SETTING

• Set PATH variable for Java:


• C:\Program Files\Java\jdk-18.0.2.1\bin
• location of JDK

• Set JUNIT_HOME
• C:\JUNIT
• Location of Junit.jar
07-02-2023

PATH SETTING

• Right Click “This PC”.


• Goto “Properties”.

PATH SETTING

• Goto Advanced Systems Settings.


07-02-2023

PATH SETTING

• Click on Environment Variables.

PATH SETTING

• Click on “Path” from System Variables.


07-02-2023

PATH SETTING

• Click on Edit->New
• Paste the location bin folder
of JDK.
• Click Ok->Ok->Ok.

RUNNING A JAVA PROGRAM

• Open NetBeans IDE.


• Click File->New Project-> Java with Ant->Java Application
07-02-2023

EXAMPLE JAVA CLASS

• /**
• * Simple Java Calculator with add and multiply method
• */
• public class Calculator {
• public int add(int... number) {
• int total = 0;
• for (int i : number) {
• total += i;
• }

EXAMPLE JAVA CLASS

• return total;
• }
• public int multiply(int... number) {
• int product = 1;
• for (int i : number) {
• product *= i;
• }
• return product;
• }
07-02-2023

EXAMPLE JAVA JUNIT TEST CLASS

• import static org.junit.Assert.*;


• import org.junit.Test;
• /**
• * JUnit Test class for testing methods of Calculator class.
• */
• public class CalculatorTest {
• @Test
• public void testAdd() {
• Calculator calc = new Calculator();

EXAMPLE JAVA JUNIT TEST CLASS

• assertEquals(60, calc.add(10,20,30));
• }
• @Test
• public void testMultiply() {
• Calculator calc = new Calculator();
• assertEquals(6000, calc.multiply(10,20,30));
• }
• }
07-02-2023

CREATING JUNIT TEST CASE

• Open NetBeans
• Go to File->New Project->Java With Ant->Java Application
• Give the name of the project (Say Square)
• Deselect “Create Main Class”
• Click on “Square” and expand it.
• Right click “default package”->new->Java Class.
• Give Class Name (Say Square)
• Click Finish

CREATING JUNIT TEST CASE

• Click Tools->Create/Update Tests


• Click Ok.
• The file SquareTest.java is created.
07-02-2023

CREATING JUNIT TEST CASE

• Right click SquareTes.java->Test File.


• If test case passed, it will show Test Case Passed.
• In case test case does not pass, it will show Test Case Failed.

SQUARE.JAVA

• public class Square {


• int Square(int a){
• return a*a;
• }
• }
07-02-2023

SQUARETEST.JAVA

• 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.*;

SQUARETEST.JAVA
• /**
• *
• * @author user
• */
• public class SquareTest {
• public SquareTest() {
• }
• @BeforeClass
• public static void setUpClass() {
• }
07-02-2023

SQUARETEST.JAVA

• @AfterClass
• public static void tearDownClass() {
• }
• @Before
• public void setUp() {
• }
• @After
• public void tearDown() {
• }

SQUARETEST.JAVA

• /**
• * Test of Square method, of class Square.
• */
• @Test
• public void testSquare() {
• System.out.println("Square");
• int a = 3;
• Square instance = new Square();
• int expResult = 9;
07-02-2023

SQUARETEST.JAVA

• int result = instance.Square(a);


• assertEquals(expResult, result);
• // TODO review the generated test code and remove the default call to fail.
• //fail("The test case is a prototype.");
• }
• }

REFERENCES

• https://www.tutorialspoint.com/junit/index.htm
• How to Install NetBeans IDE 14 on Windows 10/11 [2022].
https://www.youtube.com/watch?v=iROJ1BN55mc
• https://www.javatpoint.com/junit-tutorial
07-02-2023

ASSIGNMENT

• Write a program for a calculator that performs addition, subtraction, multiplication


and division of two numbers.
• Write a program that calculates the area and perimeter of the circle.
• Write a program which read the first name and last name from console and match
with expected result.
• Write a program that takes three double numbers from the java console representing,
respectively, the three coefficients a, b, and c of a quadratic equation and find all the
roots namely equal, different and imaginary.
• Write a program to find greatest of three numbers.
• Write a program to find factorial of number.

ASSIGNMENT

• Write a program to check whether a number is prime.


• Write a program to check whether a number is palindrome.
• Write a program to sort a list of numbers using bubble sort.
• Write a program to display transpose of a matrix.
07-02-2023

You might also like