Create & running a test class

Create & running a test class


Posted in : Java Posted on : November 22, 2010 at 7:00 PM Comments : [ 0 ]

This section contains the detail about Create & running a test class

Create & running a test class

You need to create a java code as below :

package devmanuals.junit;

import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;

public class SimpleTest {

@Test
public void testEmptyCollection() {
Collection collection = new ArrayList();
assertTrue(collection.isEmpty());
}
}

Description of the Code

You need to compile the above code before testing. The first import "org.junit" is necessary because this package contains all the necessary classes needed to implement unit testing with JUnit.

The second import is necessary to implement assertion define in the imported class. We will describe in detail about assertion later this tutorial.

@Test is an annotation which tells runner that this is the method which is under test. You need to implement it on every method under test.

The "assertTrue" is a assertion which is used to test the Boolean condition. If the provided condition is true then the test is successful otherwise if it is false, then the method will fail. Here, collection is empty thats why it returns true & test is successful in this condition.

Output

The above test is successful Given below the output after running test :

C:\Program Files\Java\junit4.8.1>java org.junit.runner.JUnitCore devmanuals.junit.SimpleTest
JUnit version 4.8.1
.
Time: 0.032

OK (1 test)

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics