nycynik
nycynik

Reputation: 7541

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

Upvotes: 5

Views: 5114

Answers (4)

anuja jain
anuja jain

Reputation: 1387

In JUnit Annotations are used to give a meaning to a method or class as per test execution point of view. Once you use @Test annotation with a method then that method is not longer an ordinary method it is a Test Case and it will be Executed as Test Case by IDE and JUnit will diplay its execution results as per test case passed or mail on the basis of your assertions.

In case you are starting with JUnit as a beginner do check out simple Junit Tutorial here - http://qaautomated.blogspot.in/p/junit.html

Upvotes: 0

Stefan Birkner
Stefan Birkner

Reputation: 24510

In JUnit 4 the @Test annotation is used to tell JUnit that a specific method is a test. In JUnit 3 in contrast every method is a test if its name starts with test and its class extends TestCase.

I assume that InstrumentationTestCase extends junit.framework.TestCase. This means that you're using a JUnit 4 annotation within a JUnit 3 test. In this case the tool that runs the tests (your IDE or build tool like Ant or Maven) decides whether is recognizes the @Test annotation or not. You can verify this by renaming testGetDate() to something that doesn't start with test, e.g. shouldReturnDate(). If your tool still runs 17 tests than you know that it supports JUnit 4 annotations within JUnit 3 tests. If it runs 16 tests than you know that the @Test annotation is just a flashbang that does nothing.

JUnit 4 still provides the classes of JUnit 3 (the junit.framework package). This means you can use JUnit 3 style tests with JUnit 4.

Upvotes: 1

Parth Solanki
Parth Solanki

Reputation: 3448

@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.

Upvotes: 7

Shaggy
Shaggy

Reputation: 1464

It identifies a method as a test method. JUnit invokes the class, then it invokes the annotated methods.

If an exception occurs, the test fails; however, you can specify that an exception should occur. If it does not, the test will fail (testing for the exception - sort of inverse testing):

@Test(expected = Exception.class) - Fails if the method does not throw the named exception.

You can also set time limits and if the function does not finish with the allotted time it will fail:

@Test(timeout = 500) - Fails if the method takes longer than 500 milliseconds.

Upvotes: 1

Related Questions