JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: List of JUnit annotations.


Annotations are introduced in JUnit4. Here are the list of annotations and its descriptions

Reference: org.junit java docs

@Test: The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.


public class MyTestClass {
    @Test 
    public void myTestMethod() {
    	/**
    	 * Use Assert methods to call your methods to be tested.
    	 * A simple test to check whether the given list is empty or not. 
    	 */
       org.junit.Assert.assertTrue( new ArrayList().isEmpty() );
    }
 }

@Test (expected = Exception.class): Sometimes we need to test the exception to be thrown by the test. @Test annotation provides a parameter called 'expected', declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails.


public class MyTestClass {
    @Test(expected=IOException.class) 
    public void myTestMethod() {
    	/**
    	 * this test performs some IO operations, sometimes we may not 
    	 * get access to the resources, then the method should through 
    	 * declared exception. 
    	 */
       ....
       ....
    }
 }

@Test(timeout=100): Somethimes we need to mesure the performance interms of time. The @Test annotations provides an optional parameter called 'timeout', which causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds).


public class MyTestClass {
    @Test@Test(timeout=100)
    public void myTestMethod() {
    	/**
    	 * The IO operation has to be done with in 100 milli seconds. If not,
    	 * the test should fail. 
    	 */
       ....
       ....
    }
 }

@Before: When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of super classes will be run before those of the current class.


public class MyTestClass {
    
    List<String> testList;
    
    @Before 
    public void initialize() {
    	testList = new ArrayList<String>();
    }
    
    @Test 
    public void myTestMethod() {
    	/**
    	 * Use Assert methods to call your methods to be tested.
    	 * A simple test to check whether the given list is empty or not. 
    	 */
       org.junit.Assert.assertTrue( testList.isEmpty() );
    }
 }

@After: If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method. All @After methods are guaranteed to run even if a Before or Test method throws an exception. The @After methods declared in superclasses will be run after those of the current class.


public class MyTestClass {
    
    OutputStream stream;
    
    @Before 
    public void initialize() {
    	/**
    	 * Open OutputStream, and use this stream for tests.
    	 */
    	stream = new FileOutputStream(...);
    }
    
    @Test 
    public void myTestMethod() {
    	/**
    	 * Now use OutputStream object to perform tests 
    	 */
       ...
       ...
    }
    
    
    @After 
    public void closeOutputStream() {
          /**
           * Close output stream here
           */
          try{
          	if(stream != null) stream.close();	
          } catch(Exception ex){
          	
          }
    }
 }

@BeforeClass: Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

The annotations @BeforeClass and @Before are same in functionality. The only difference is the method annotated with @BeforeClass will be called once per test class based, and the method annotated with @Before will be called once per test based.


public class MyTestClass {
    
    @BeforeClass 
    public void initGlobalResources() {
    	/**
    	 * This method will be called only once per test class. 
    	 */
    }
    
    @Before 
    public void initializeResources() {
    	/**
    	 * This method will be called before calling every test. 
    	 */
    }
    
    @Test 
    public void myTestMethod1() {
    	/**
    	 * initializeResources() method will be called before calling this method  
    	 */
    }
    
    @Test 
    public void myTestMethod2() {
    	/**
    	 * initializeResources() method will be called before calling this method  
    	 */
    }
 }

@AfterClass: If you allocate expensive external resources in a BeforeClass method you need to release them after all the tests in the class have run. Annotating a public static void method with @AfterClass causes that method to be run after all the tests in the class have been run. All @AfterClass methods are guaranteed to run even if a BeforeClass method throws an exception. The @AfterClass methods declared in superclasses will be run after those of the current class.

The annotations @AfterClass and @After are same in functionality. The only difference is the method annotated with @AfterClass will be called once per test class based, and the method annotated with @After will be called once per test based.


public class MyTestClass {
    
    @BeforeClass 
    public void initGlobalResources() {
    	/**
    	 * This method will be called only once per test class. It will be called
    	 * before executing test.
    	 */
    }
    
    @Test 
    public void myTestMethod1() {
    	// write your test code here...
    	...
    	...
    }
    
    @BeforeClass 
    public void closeGlobalResources() {
    	/**
    	 * This method will be called only once per test class. It will be called 
    	 * after executing test.  
    	 */
    }
 }

@Ignore: Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed. Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and the number of tests that failed.

You can also use @Ignore annotation at class level.


public class MyTestClass {
    @Ignore
    @Test 
    public void myTestMethod() {
    	/**
    	 * This test will be ignored. 
    	 */
       org.junit.Assert.assertTrue( new ArrayList().isEmpty() );
    }
 }

<< Previous Program | Next Program >>

Java JUnit Examples

  1. Simple JUnit test using @Test annotation.
  2. List of JUnit annotations.
  3. Assertion method Assert.assertArrayEquals() example.
  4. How to do JUnit test for comapring two list of user defined objects?
  5. Assertion method Assert.assertEquals() example.
  6. Assertion method Assert.assertFalse() example.
  7. Assertion method Assert.assertTrue() example.
  8. Assertion method Assert.assertNotNull() example.
  9. Assertion method Assert.assertNull() example.
  10. Assertion method Assert.assertNotSame() example.
  11. Assertion method Assert.assertSame() example.
Knowledge Centre
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.
Famous Quotations
I don’t know the key to success, but the key to failure is trying to please everybody.
-- Bill Cosby

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.