Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the JUnit 4 @Test Annotation actually do

What does the @Test actually do? I have some tests without it, and they run fine.

My class starts with

public class TransactionTest extends InstrumentationTestCase {

The test runs with either:

public void testGetDate() throws Exception {

or

@Test
public void testGetDate() throws Exception {

EDIT: It was pointed out that I may be using JUnit 3 tests, but I think I am using JUnit 4:

enter image description here

like image 953
nycynik Avatar asked Apr 02 '15 17:04

nycynik


1 Answers

@Test 
public void method()

@Test => annotation identifies a method as a test method.
@Test(expected = Exception.class) => Fails if the method does not throw the named exception.
@Test(timeout=100)  =>  Fails if the method takes longer than 100 milliseconds.

@Before 
public void method() =>This method is executed before each test. It is used to prepare the test environment (e.g., read input data, initialize the class).

@After 
public void method() => This method is executed after each test. It is used to cleanup the test environment (e.g., delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures.

@BeforeClass 
public static void method() => This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example, to connect to a database. Methods marked with this annotation need to be defined as static to work with JUnit.

@AfterClass 
public static void method() =>  This method is executed once, after all tests have been finished. It is used to perform clean-up activities, for example, to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit.

@Ignore => Ignores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.
like image 112
Parth Solanki Avatar answered Oct 21 '22 14:10

Parth Solanki