It is an open source Unit testing framework for Java. It is used to test small chunk of code. Developers who are following test-driven methodology must write and execute unit test first before any code. JUnit promotes the idea of 'first testing then coding', which emphasizes on setting up the test data for a piece of code that can be tested first and then implemented.
● JUnit is useful for developers, who work in a test-driven environment.
● It finds bugs early in the code, which makes code more reliable.
● Unit testing forces a developer to read code more than writing.
Following are the features of JUnit:
● JUnit is an open source framework.
● 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 of the code.
● JUnit is elegantly simple. It is less complex and takes less time.
● In JUnit we can easily identify the passed and failed test cases.
Download the latest version of JUnit jar file and set the JUNIT_HOME environment variable to point to the base directory location where JUNIT jar is stored on your machine. Then set the CLASSPATH environment variable to point to the JUNIT jar location.
Yes, JUnit Framework can be easily integrated with either of the following:
● Eclipse
● Ant
● Maven
Fixtures is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well-known & fixed environment in which tests are run so that results are repeatable.
Fixtures include following methods:
● setUp() method, which runs before every test invocation.
● tearDown() method, which runs after every test method.
Here is the code snippet:
import junit.framework.*; public class JavaTest extends TestCase { protected int val1, val2; // assigning the values protected void setUp(){ val1 = 3; val2 = 3; } // test method to add two values public void testAdd(){ double result = val1 + val2; assertTrue(result == 6); } }
A test suite bundles a few unit test cases and runs them together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test.
Here is the code snippet:
import org.junit.runner.RunWith; import org.junit.runners.Suite; //JUnit Suite Test sample @RunWith(Suite.class) @Suite.SuiteClasses({ TestJunit1.class ,TestJunit2.class }) public class JunitTestSuite { }
Test runner is used for executing the test cases.
Here is the code snippet:
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(TestJunit.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
Following are some of the important JUnit classes:
● Assert ? Contains a set of assert methods.
● TestCase ? Contains a test case that defines the fixture to run multiple tests.
● TestResult ? Contains methods to collect the results of executing a test case.