JUnit 4.x – Testing Framework : JUnit API
JUnit Framework API |
we have to understand following in order to use JUnit for unit testing
- The class hierarchy of junit.framework Package
- What is a TestCase Class
- Working with TestCase Class
- What are assert methods
- Defining and adding necessary codes to assert methods
- The programmer has to create a JUnit Test Class (that implements the test method for testing the existing method) – This is achieved by inheriting (extends) JUnit’s TestCase class
- Define assert methods -The assert method of TestCase class is overridden by calling programmer’s Code for comparing the actual value with the expected value
What is a TestCase?
- A TestCase is a class defined in junit.framework
- Fixture are defined in TestCase to run multiple tests
- It is meant for defining test cases.
Calculator class that helps to add two number.
Calculator.java
package com.javaskool;
public class Calculator {
public int add(int a,int b)
{
return a+b;
}
}
CalcTest class that helps to test my Calculator class.
CalcTest.java
package com.javaskool;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CalcTest {
@Before
public void setUp() throws Exception {
System.out.println("Before");
}
@After
public void tearDown() throws Exception {
System.out.println("After");
}
@Test
public void testAdd() {
Calculator c=new Calculator();
int x=c.add(50, 20);
assertEquals(70, x); //Here 70 is Expected value and x is the Actual value that would come from addition.
System.out.println(x);
}
}
Working with TestCase:
- To define a Test case class it should
- Be a subclass of TestCase
- Define instance variables
- Initialize the fixture state by overriding setUp
- Clean-up after a test
- In its own fixture so there can be no side effects among test runs.
Assert methods
- Assert methods are usually used to compare the actual value with the expected value.
- All assert methods are static methods
- Return type of all assert methods are void.
- Assert types
- assertEquals()
- assertTrue()
- assertFalse(), etc…
Method | Description |
---|---|
assertEquals (boolean expected, boolean actual) | It compares 2 Boolean values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals(byte expected, byte actual) | It compares 2 byte values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals(char expected, char actual) | It compares 2 character values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals(double expected, double actual, double delta) | It compares 2 double values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals(float expected, float actual, float delta) | It compares 2 float values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals(int expected, int actual) | It compares 2 integer values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals(long expected, long actual) | It compares 2 long values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals(Object expected, Object actual) | It compares 2 Objects values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals(short expected, short actual) | It compares 2 short values and checks if the values are equal or not. If the values are not equal then it throws AssertionFailedError exception |
assertEquals (String message, boolean expected, boolean actual) | It compares 2 Boolean values and checks if the values are equal or not. If the values are not equal then it prints the exception message followed by the string |
assertEquals (String message, byte expected, byte actual) | It compares 2 byte values and checks if the values are equal or not. If the values are not equal then it prints the exception message followed by the string |
Syntax:
assertEquals(<datatype>,<datatype>,[<datatype>])
TestSuite
- A TestSuite is a Composite of Tests
- TestSuite runs a collection of test cases
Example:
TestSuite ts1 = new TestSuite();
ts1.addTest(new MyTest("testAdd"));
ts1.addTest(new MyTest("testDivide"));
TestResult
- The results of executing a test case is collected by an Instance of TestResult.
- The test framework differentiates between failures and errors.
- A failure is expected and checked for with assertions.
- Errors are unexpected problems like an ArrayIndexOutOfBoundsException
TestRunner
- TestRunner is a Pre-defined class in JUnit package.
- It is meant for running either TestCase or TestSuite.
- It is a subclass of BaseTestRunner and implements TestListener interface.
- TestRunner class of junit.swingui. Package implements TestRunContext.
- Types of TestRunner are :
Type | Syntax |
---|---|
text based | java junit.textui.TestRunner TestCaseClassName / TestSuiteName |
swing based | java junit.swingui.TestRunner TestCaseClassName / TestSuiteName |
ant | ant |
Downloads Examples |
Recent Comments