What JUnit 4 @Test Annotation does

What does @Test actually do? I have several tests without it and they work fine.

My class starts with

public class TransactionTest extends InstrumentationTestCase {

The test is performed using:

public void testGetDate() throws Exception {

or

@Test
public void testGetDate() throws Exception {

EDIT: It has been pointed out that I can use JUnit 3 tests, but I think I use JUnit 4:

enter image description here

+4
source share
4 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.
+7
source

He identifies the method as a testing method. JUnit calls the class, then it calls annotated methods.

If an exception occurs, the test fails; however, you can indicate that an exception should occur. If this is not the case, the test will fail (testing for exception is a kind of reverse testing):

@Test (expected = Exception.class) - , .

, , :

@Test (timeout = 500) - , 500 .

+1

JUnit 4 @Test , JUnit, . JUnit 3, , , test, TestCase.

, InstrumentationTestCase junit.framework.TestCase. , JUnit 4 JUnit 3. , ( IDE , Ant Maven), , @Test . , testGetDate() , test, . shouldReturnDate(). - 17 , , JUnit 4 JUnit 3. 16 , , @Test - -, .

JUnit 4 - JUnit 3 ( junit.framework). , JUnit 3 JUnit 4.

+1

JUnit , . , @Test , , , IDE, JUnit .

JUnit , Junit - http://qaautomated.blogspot.in/p/junit.html

0

All Articles