Professional Documents
Culture Documents
TOPICS TO BE DISCUSSED
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
•It finds bugs early in the code, which makes our code
more reliable.
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
• 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
• 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.
• The Junit 4.x framework is annotation based, so let's see the annotations that can be
used while writing the test cases.
ASSERT CLASS
PATH SETTING
• Set JUNIT_HOME
• C:\JUNIT
• Location of Junit.jar
07-02-2023
PATH SETTING
PATH SETTING
PATH SETTING
PATH SETTING
PATH SETTING
• Click on Edit->New
• Paste the location bin folder
of JDK.
• Click Ok->Ok->Ok.
• /**
• * 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;
• }
• return total;
• }
• public int multiply(int... number) {
• int product = 1;
• for (int i : number) {
• product *= i;
• }
• return product;
• }
07-02-2023
• 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
• 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
SQUARE.JAVA
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
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
ASSIGNMENT