Monday, January 10, 2011

JUnit Annotation Type - Before

3. Annotation Type Before: 

When writing tests, it is common to find that several tests need similar object 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 superclasses will be run before those of the current class.

Here is a simple example:



public class Example{
       List empty;
       @Before
        public void initialize(){
               empty = new ArrayList();
         }
         @Test public void size(){
             ...
         }
          @Test public void remove(){
             ...
        }
}

No comments:

Post a Comment