May 15, 2021 jUnit
The most important package in JUnit is
junit.framework
contains all the core classes.
Some important classes are listed below:
Serial number | The name of the class | The functionality of the class |
---|---|---|
1 | Assert |
assert
of assert methods
|
2 | TestCase | A fixture that defines how to run multiple tests |
3 | TestResult |
TestResult
all the results of performing a test sample
|
4 | TestSuite |
TestSuite
a collection of tests
|
Here's
org.junit.Assert
class:
public class Assert extends java.lang.Object
This class provides a series of useful declaration methods for writing tests. O
nly failed claims methods are logged.
Assert
method columns for the Assert class are as follows:
Serial number | Method and description |
---|---|
1 |
void assertEquals(boolean expected, boolean actual)
Check that the two variables or the equals are balanced |
2 |
void assertFalse(boolean condition)
The check conditions are false |
3 |
The check object is not empty |
4 |
The check object is empty |
5 |
The check condition is true |
6 |
Make the test fail without reporting |
Let's test some of the methods mentioned above in the example. I
n
C:
Create a JUNIT_WORKSPACE called
TestJunit1.java
in the .java directory.
import org.junit.Test;
import static org.junit.Assert.*;
public class TestJunit1 {
@Test
public void testAdd() {
//test data
int num= 5;
String temp= null;
String str= "Junit is working fine";
//check for equality
assertEquals("Junit is working fine", str);
//check for false condition
assertFalse(num > 6);
//check for not null value
assertNotNull(str);
}
}
Next,
we'll be at C:
The test case JUNIT_WORKSPACE a
class named
TestRunner1.java
in the .java the testrunner directory to execute.
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner1 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit1.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
The Test case and
Test case
are
Test Runner
C:\JUNIT_WORKSPACE>javac TestJunit1.java TestRunner1.java
Now run
Test Runner
it will run the test
Test Case
defined and provided in the Test Case class.
C:\JUNIT_WORKSPACE>java TestRunner1
Check the output.
true
Here's
org.junit.TestCaset
class:
public abstract class TestCase extends Assert implements Test
The test sample defines a fixed format for running multiple tests.
Some of the important methods of the
TestCase
class are listed as follows:
Serial number | Method and description |
---|---|
1 |
int countTestCases()
Count of test cases executed for run (TestResult result). |
2 |
TestResult createResult()
Create a default TestResult object |
3 |
String getName()
Gets the name of TestCase |
4 |
TestResult run()
A convenient way to run this test and collect the results produced by the TestResult object |
5 |
void run(TestResult result)
Run the test case in TestResult and collect the results |
6 |
void setName(String name)
Set the name of TestCase |
7 |
void setUp()
Create a fixture, for example, to turn on a network connection |
8 |
void tearDown()
Remove the fixture, for example, to turn off a network connection |
9 |
String toString()
Returns a string representing the test case |
Let's try the method mentioned above in the example. I
n
C:
Create a JUNIT_WORKSPACE called
TestJunit2.java
under the .java path.
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
public class TestJunit2 extends TestCase {
protected double fValue1;
protected double fValue2;
@Before
public void setUp() {
fValue1= 2.0;
fValue2= 3.0;
}
@Test
public void testAdd() {
//count the number of test cases
System.out.println("No of Test Case = "+ this.countTestCases());
//test getName
String name= this.getName();
System.out.println("Test Case Name = "+ name);
//test setName
this.setName("testNewAdd");
String newName= this.getName();
System.out.println("Updated Test Case Name = "+ newName);
}
//tearDown used to close the connection or clean up activities
public void tearDown( ) {
}
}
Next,
in C:
The test case JUNIT_WORKSPACE a
class called
TestRunner2.java
.java under the path to execute the test case.
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner2 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit2.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
The Test case and
Test case
are
Test Runner
C:\JUNIT_WORKSPACE>javac TestJunit2.java TestRunner2.java
Now run
Test Runner
it will run the test
Test Case
defined and provided in the Test Case class.
C:\JUNIT_WORKSPACE>java TestRunner2
Check the output.
No of Test Case = 1
Test Case Name = testAdd
Updated Test Case Name = testNewAdd
true
The following is
org.junit.TestResult
class:
public class TestResult extends Object
TestResult
the results of all execution test cases. I
t is an example of the collection parameter plane. T
his experimental framework distinguishes between failure and error. F
ailures are predictable and can be checked by assumptions. E
rrors are unpredictable problems
ArrayIndexOutOfBoundsException
Some of the important methods of the
TestResult
class are listed as follows:
Serial number | Method and description |
---|---|
1 |
Add an error to the error list |
2 |
Add a failure to the list of failures |
3 |
Displays the results of the test being compiled |
4 |
int errorCount()
Gets the number of errors detected |
5 |
Enumeration errors()
Returns the details of the error |
6 |
int failureCount()
Gets the number of failures detected |
7 |
void run(TestCase test
runs
TestCase
|
8 |
int runCount()
Gets the number of tests to run |
9 |
void startTest(Test test)
Declare that a test is about to begin |
10 |
void stop()
Indicates that the test must be stopped |
In
C:
Create a JUNIT_WORKSPACE called
TestJunit3.java
under the .java path.
import org.junit.Test;
import junit.framework.AssertionFailedError;
import junit.framework.TestResult;
public class TestJunit3 extends TestResult {
// add the error
public synchronized void addError(Test test, Throwable t) {
super.addError((junit.framework.Test) test, t);
}
// add the failure
public synchronized void addFailure(Test test, AssertionFailedError t) {
super.addFailure((junit.framework.Test) test, t);
}
@Test
public void testAdd() {
// add any test
}
// Marks that the test run should stop.
public synchronized void stop() {
//stop the test here
}
}
Next,
in C:
The test case JUNIT_WORKSPACE a
class called
TestRunner3.java
under the path to execute the test case.
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner3 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit3.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
The Test case and
Test case
are
Test Runner
C:\JUNIT_WORKSPACE>javac TestJunit3.java TestRunner3.java
Now run
Test Runner
it will run the test
Test Case
defined and provided in the Test Case class.
C:\JUNIT_WORKSPACE>java TestRunner3
Check the output.
true
The following is
org.junit.TestSuite
class:
public class TestSuite extends Object implements Test
TestSuite
class is part of the test. I
t runs a lot of test cases.
Some important method columns for the
TestSuite
class are as follows:
Serial number | Method and description |
---|---|
1 |
void addTest(Test test)
Add a test to the set. |
2 |
void addTestSuite(Class<? extends TestCase> testClass)
Add tests from a class that has already been given to the set. |
3 |
int countTestCases()
Count the test cases that the test is about to run. |
4 |
String getName()
Returns the name of the set. |
5 |
void run(TestResult result)
Run tests and collect results in TestResult. |
6 |
void setName(String name)
The name of the set. |
7 |
Test testAt(int index)
The test is returned in the given directory. |
8 |
int testCount()
Returns the number of tests in the set. |
9 |
static Test warning(String message)
Returns a test that will fail and logs a warning message. |
In
C:
Create a JUNIT_WORKSPACE called
JunitTestSuite.java
under the .java path.
import junit.framework.*;
public class JunitTestSuite {
public static void main(String[] a) {
// add the test's in the suite
TestSuite suite = new TestSuite(TestJunit1.class, TestJunit2.class, TestJunit3.class );
TestResult result = new TestResult();
suite.run(result);
System.out.println("Number of test cases = " + result.runCount());
}
}
Compile Test
Test suit
C:\JUNIT_WORKSPACE>javac JunitTestSuite.java
Now run
Test Suit
C:\JUNIT_WORKSPACE>java JunitTestSuite
Check the output.
No of Test Case = 1
Test Case Name = testAdd
Updated Test Case Name = testNewAdd
Number of test cases = 3