0% found this document useful (0 votes)
109 views3 pages

JUnit Annotations Explained

The @Before annotation causes a method to run before each test method. The @After annotation causes a method to run after each test method. The @Ignore annotation allows tests to be temporarily skipped. The @Test annotation identifies methods that represent test cases. The @RunWith annotation specifies an alternative test runner class to use instead of the default runner.

Uploaded by

Manu Khullar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views3 pages

JUnit Annotations Explained

The @Before annotation causes a method to run before each test method. The @After annotation causes a method to run after each test method. The @Ignore annotation allows tests to be temporarily skipped. The @Test annotation identifies methods that represent test cases. The @RunWith annotation specifies an alternative test runner class to use instead of the default runner.

Uploaded by

Manu Khullar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Annotation Type 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. Here is a simple example:
public class Example { File output; @Before public void createOutputFile() { output= new File(...); } @Test public void something() { ... } @After public void deleteOutputFile() { output.delete(); } }

2. Annotation Type 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 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() { ... } }

3. Annotation Type 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. For example:
@Ignore @Test public void something() { ...

@Ignore takes an optional default parameter if you want to record why a test is being ignored:
@Ignore("not ready yet") @Test public void something() { ...

@Ignore can also be applied to the test class:


@Ignore public class IgnoreMe { @Test public void test1() { ... } @Test public void test2() { ... } }

4. Annotation Type 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. A simple test looks like this:
public class Example { @Test public void method() { org.junit.Assert.assertTrue( new ArrayList().isEmpty() ); } }

The Test annotation supports two optional parameters. The first, 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. For example, the following test succeeds:

@Test(expected=IndexOutOfBoundsException.class) public void outOfBounds() } new ArrayList<Object>().get(1);

The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds). The following test fails:
@Test(timeout=100) public void infinity() { while(true); }

5. Annotation Type RunWith

When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit. We added this feature late in development. While it seems powerful we expect the runner API to change as we learn how people really use it. Some of the classes that are currently internal will likely be refined and become public. For example, suites in JUnit 4 are built using RunWith, and a custom runner named Suite:
@RunWith(Suite.class) @SuiteClasses(ATest.class, BTest.class, CTest.class) public class ABCSuite { }

You might also like