The Procedure to create a JUnit Test Class is as follows:
1. There are many ways to create a JUnit Test Case Class.
- Select File > New > JUnit Test Case
- Select the arrow of the New icon (shown at the end of the post) in the upper left of the toolbar. Select JUnit Test Case,
- Right click on a package in the Package Explorer view in the Java Perspective, and select JUnit Test Case, or
- Click on the arrow of the New Java Class icon (shown at the end of the post) in the toolbar. Select JUnit Test Case.
- You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestCase as the super class of the test class you are creating.
- Right click on an application class that you want to create a test class for, and select New > JUnit Test Case.
2. In the wizard for creating a new JUnit test case classes
- The source folder specifies where you want to create the test case. Based on the project hierarchy presented in previous post, we want to create the new test case in the tests/ source folder.(in our case 'ExampleJunit/tests' )
- Next, specify the package you want your test case to belong to. If you are associating your test case with an application class, then you will want both of these classes to share the same package so you can take advantage of package level visibility when testing your application class.(in our case 'test' )
- Specify the test case name. If you are associating your test case with an application class the convention is that the test case name is the application class name followed by Test.(in our case 'SampleTest')
- JUnit has several standard methods that are useful for testing. setUp() and tearDown() methods are run before and after each test method.
- If you are associating your test case with an application class, then Browse for the application class in the Class under test field.
Note:All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit. If the method in the class under test is protected, the test class must be in the same package.
- import junit.framework.TestCase;
- public class SampleTest extends TestCase {
- /**
- * Sets up the test fixture.
- * (Called before every test case method.)
- */
- protected void setUp() {
- }
- /**
- * Tears down the test fixture.
- * (Called after every test case method.)
- */
- protected void tearDown() {
- emptyList = null;
- }
- public void testSomeBehavior() {
- assertEquals("Empty list should have 0 elements", 0, emptyList.size());
- }
- public void testForException() {
- try {
- fail("Should raise an IndexOutOfBoundsException");
- }
- }
- }
- }
5. Below is a test case template based on the examples presented in the Junit 4.This test class demonstrates the basic functionality of the setUp() and tearDown() methods, and gives example test cases.The testForException() method demonstrates how to test that an exception is properly thrown.
Note: All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit. If the method in the class under test is protected, the test class must be in the same package.
- import org.junit.*;
- import static org.junit.Assert.*;
- public class SampleTest {
- /**
- * Sets up the test fixture.
- * (Called before every test case method.)
- */
- @Before
- public void setUp() {
- }
- /**
- * Tears down the test fixture.
- * (Called after every test case method.)
- */
- @After
- public void tearDown() {
- emptyList = null;
- }
- @Test
- public void testSomeBehavior() {
- assertEquals("Empty list should have 0 elements", 0, emptyList.size());
- }
- public void testForException() {
- }
- }
|
New |
New Java Class |
No comments:
Post a Comment