JUnit API - JUnit

What is JUnit API?

Here junit.framework is the most important package in JUnit which contains all the core classes. Some of the important classes are as follows −
Sr.No. Class Name Functionality
1 Assert A set of assert methods.
2 TestCase A test case defines the fixture to run multiple tests.
3 TestResult A TestResult collects the results of executing a test case.
4 TestSuite A TestSuite is a composite of tests.

Assert Class

Here’s the declaration for org.junit.Assert class –
Assert class includes a set of assertion methods useful for writing tests. But only failed assertions are recorded. Some of the important methods of Assert class are as follows −
Sr.No. Methods & Description
1

void assertEquals(boolean expected, boolean actual)

Checks that two primitives/objects are equal.

2

void assertFalse(boolean condition)

Checks that a condition is false.

3

void assertNotNull(Object object)

Checks that an object isn't null.

4

void assertNull(Object object)

Checks that an object is null.

5

void assertTrue(boolean condition)

Checks that a condition is true.

6

void fail()

Fails a test with no message.

Let's use some of the above-mentioned methods in an example. Create a java class file named TestJunit1.java in C:\>JUNIT_WORKSPACE.
Next, create a java class file named TestRunner1.java in C:\>JUNIT_WORKSPACE to execute test case(s).
Let’s compile the test case and Test Runner classes using javac.
Now run the Test Runner, which will run the test case defined in the provided Test Case class.
Verify the output.

TestCase Class

Here’s the declaration for org.junit.TestCase class –
A test case is known as the fixture to run multiple tests. Some of the important methods of TestCase class are as follows −
Sr.No. Methods & Description
1

int countTestCases()

Counts the number of test cases executed by run(TestResult result).

2

TestResult createResult()

Creates a default TestResult object.

3

String getName()

Gets the name of a TestCase.

4

TestResult run()

A convenience method to run this test, collecting the results with a default TestResult object.

5

void run(TestResult result)

Runs the test case and collects the results in TestResult.

6

void setName(String name)

Sets the name of a TestCase.

7

void setUp()

Sets up the fixture, for example, open a network connection.

8

void tearDown()

Tears down the fixture, for example, close a network connection.

9

String toString()

Returns a string representation of the test case.

Now perform some of the above-mentioned methods in an example. Create a java class file named TestJunit2.java in C:\>JUNIT_WORKSPACE.
Next, create a java class file named TestRunner2.java in C:\>JUNIT_WORKSPACE to execute test case(s).
Let’s compile the test case and Test Runner classes using javac.
Now run the Test Runner, which will run the test case defined in the provided Test Case class.
Verify the output.

TestResult Class

Here’s the declaration for org.junit.TestResult class –
A TestResult includes the results of executing a test case. It is a package of the collecting Parameter pattern. The test framework differs from failures to errors. A failure is anticipated and checked for with assertions. Errors are unanticipated problems like an ArrayIndexOutOfBoundsException. Some of the important methods of TestResult class are as follows −
Sr.No. Methods & Description
1

void addError(Test test, Throwable t)

Adds an error to the list of errors.

2

void addFailure(Test test, AssertionFailedError t)

Adds a failure to the list of failures.

3

void endTest(Test test)

Informs the result that a test was completed.

4

int errorCount()

Gets the number of detected errors.

5

Enumeration<TestFailure> errors()

Returns an Enumeration for the errors.

6

int failureCount()

Gets the number of detected failures.

7

void run(TestCase test)

Runs a TestCase.

8

int runCount()

Gets the number of run tests.

9

void startTest(Test test)

Informs the result that a test will be started.

10

void stop()

Marks that the test run should stop.

Create a java class file named TestJunit3.java in C:\>JUNIT_WORKSPACE.
Next, create a java class file named TestRunner3.java in C:\>JUNIT_WORKSPACE to execute test case(s).
lETCompile the test case and Test Runner classes using javac.
Now run the Test Runner, which will run the test case defined in the provided Test Case class.
Verify the output.

TestSuite Class

Following is the declaration for org.junit.TestSuite class:
A TestSuite is a Composite of tests. It runs a collection of test cases. Some of the important methods of TestSuite class are as follows −
Sr.No. Methods & Description
1

void addTest(Test test)

Adds a test to the suite.

2

void addTestSuite(Class<? extends TestCase> testClass)

Adds the tests from the given class to the suite.

3

int countTestCases()

Counts the number of test cases that will be run by this test.

4

String getName()

Returns the name of the suite.

5

void run(TestResult result)

Runs the tests and collects their result in a TestResult.

6

void setName(String name)

Sets the name of the suite.

7

Test testAt(int index)

Returns the test at the given index.

8

int testCount()

Returns the number of tests in this suite.

9

static Test warning(String message)

Returns a test which will fail and log a warning message.

Create a java class file named JunitTestSuite.java in C:\>JUNIT_WORKSPACE to create Test suite.
Compile the Test suite classes using javac.
Now run the Test Suite.
Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JUnit Topics