JUnit Features
JUnit Features
JUnit is a unit testing framework for the Java programming language and it became important in the development of test-driven development. Annotations are provided by Apache Foundation.
Features of JUnit
JUnit promotes the idea of testing for developers which emphasis on setting up the test data for a piece of code which can be tested first and can be implemented. Some of the features are as follows:
- JUnit is an open source framework which is used for writing test cases and executing them
- It Provides Annotation support to identify the test methods
- Provides Assertions for comparing actual and expected results
- JUnit is very simple and easy to use and requires less time
- JUnit provides the results after executing the results with green and red status for Pass and Failed test cases
- Provides test runners for running tests
Earlier to JUnit
- Developer has to perform unit testing for the program which he/she has developed to make sure every thing is working fine.
- Before Junit framework, if developer want to do the unit testing then he needs to prepare developer test cases and it contains lot of test steps to do.
- If developers executes all the test cases on to the application with developer test cases then that process is called as manual unit test case.
- When Developer do the manual unit testing then he needs to spend lot of effort to prepare the test cases in the form of excel sheets and execute the test cases.
- If the project is enhancement project, developer needs to spend more time on to the unit testing rather then coding to resolve the problem.
Junit is a unit testing framework, by using this framework you can do the unit testing. To write Junit test cases or junit classes, junit framework provides one class called “Testcase”. TestCase:-If we want to write our own test cases.
Example JUnit program
<span class="hljs-keyword">import</span> org.junit.*;
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SampleTest</span>{</span>
<span class="hljs-annotation">@BeforeClass</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUpClass</span>() <span class="hljs-keyword">throws</span> Exception {
System.out.println(<span class="hljs-string">"In before class method"</span>);
}
<span class="hljs-annotation">@Before</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUp</span>() <span class="hljs-keyword">throws</span> Exception {
System.out.println(<span class="hljs-string">"In Before test method"</span>);
}
<span class="hljs-annotation">@Test</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testcaseOne</span>() {
System.out.println(<span class="hljs-string">"In in First test case"</span>);
}
<span class="hljs-annotation">@Test</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testcaseTwo</span>() {
System.out.println(<span class="hljs-string">"In second test case"</span>);
}
<span class="hljs-annotation">@After</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">tearDown</span>() <span class="hljs-keyword">throws</span> Exception {
System.out.println(<span class="hljs-string">"In After test method"</span>);
}
<span class="hljs-annotation">@AfterClass</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">tearDownClass</span>() <span class="hljs-keyword">throws</span> Exception {
System.out.println(<span class="hljs-string">"In After class method"</span>);
}
}