Java-Junit Framework- with example

JUnit is an open source Unit Testing Framework for JAVA. It is useful for Java Developers to write and run repeatable tests. Erich Gamma and Kent Beck initially develop it. It is an instance of xUnit architecture. As the name implies, it is used for Unit Testing of a small chunk of code.Developers who are following test-driven methodology must write and execute unit test first before any code.
Once you are done with code, you should execute all tests, and it should pass. Every time any code is added, you need to re-execute all test cases and makes sure nothing is broken.

Basic Annotations for Junit testing

  1. @BeforeClass – Run once before any of the test methods in the class, public static void
  2. @AfterClass – Run once after all the tests in the class have been run, public static void
  3. @Before – Run before @Test, public void
  4. @After – Run after @Test, public void
  5. @Test – This is the test method to run, public void

Assert class

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

Methods of Assert class

  1. void assertEquals(boolean expected,boolean actual): checks that two primitives/objects are equal. It is overloaded.
  2. void assertTrue(boolean condition): checks that a condition is true.
  3. void assertFalse(boolean condition): checks that a condition is false.
  4. void assertNull(Object obj): checks that object is null.
  5. void assertNotNull(Object obj): checks that object is not null.

JUnit Example:

Let's see the directory structure of this example.




Logic Class


package com.knowledgefactory.service;


public class Logic {


public int addition(int firstnumber, int secondnumber) {
return (firstnumber + secondnumber);
}

public int substraction(int firstnumber, int secondnumber) {
return (firstnumber - secondnumber);
}
}


Junit Class


package com.knowledgefactory.testcase;

import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.knowledgefactory.service.Logic;

public class UnitTest {

static Logic cal = null;
int sum;
int difference;

// Run once before any of the test methods in the class, public static void
@BeforeClass
public static void setUpBeforeClass() throws Exception {

cal = new Logic();
}

// Run before @Test, public void
@Before
public void setUpAddition() throws Exception {

sum = cal.addition(3, 2);
}

// Run before @Test, public void
@Before
public void setUpDifference() throws Exception {
difference = cal.substraction(3, 2);
}

// This is the test method to run, public void
@Test
public void additionTest() {

assertEquals(5, sum);
}

// This is the test method to run, public void
@Test
public void differenceTest() {
// Calculator myUnit = new Calculator();
assertEquals(1, difference);
}

@After
public void tearDown() throws Exception {
// Run after @Test, public void
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
// Run once after all the tests in the class have been run, public static void
}
}


To run this example, right click on UnitTest class -> Run As -> 2 Junit Test.






This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments

  1. Genuine and Free jobs!
    Don't miss any job opportunities!
    Improve your life, only one step!

    More Java jobs info, Please join the Java Jobs group:
    https://chat.whatsapp.com/HLFm93tkHQR4zu8hafpqSl

    ReplyDelete

Post a Comment