JUNIT4 Annotations Basic Introduction

Source: Internet
Author: User

@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 this method to is run after the Test method. All @After methods is guaranteed to run even if a before or Test method throws an exception. The @After methods declared in Superclasses'll be run after those of the current class.

If additional resources are allocated in the @before annotation method, the allocated resources need to be freed after the test is executed.

Using @after annotations A public void method causes the method to be executed after the @test annotation method executes

Even if an exception is thrown in the @before annotation method, @Test annotation method, all @after annotation methods are still executed, see example

The @after annotation method in the parent class is executed after the child class @after annotation method executes

public class Mathtest {
	@Before public
	void SetUp () throws Exception {
		throw new Exception ();
	}

	@Test public
	void Testadd () {
		math m = new Math ();
		Asserttrue (M.add (1, 1) = = 2);
	}
	
	@After public
	void TearDown () throws Exception {
		System.out.println (' after ');
	}
}
After

@AfterClass

IF you allocate expensive external resources in a before Class method you need to release them after all the tests in the Class has run. annotating a public static void method with @AfterClass causes this method to is run after all the tests in the class has been run. All @AfterClass methods is guaranteed to run even if a before Class method throws an exception. The @AfterClass methods declared in Superclasses'll be run after those of Thecurrent class.

If an expensive additional resource is allocated in the @beforeclass annotation method, the allocated resource needs to be freed after all the test methods in the test class have been executed.

Using @afterclass annotations A public static void method causes the method to be executed after all the test methods in the test class have finished executing

Even if an exception is thrown in the @beforeclass annotation method, all @afterclass annotation methods will still be executed

The @afterclass annotation method in the parent class is executed after the child class @afterclass annotation method executes

@Before

When writing tests, it's common to find that several tests need similar objects created before they can run. annotating a public void method with @Before causes this method to be run before the Test method. The @Before methods of superclasses would be run before those of the current class. No other ordering is defined.

When writing test methods, it is often found that some methods need to create the same object before execution

Using @before annotations A public void method causes the method to execute before the @test annotation method is executed (then you can create the same object in the method)

The @before annotation method of the parent class executes before the @before annotation method of the subclass executes

@BeforeClass

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. annotating a public static void No-arg method with @BeforeClass causes it-be run once before any of the test methods in The class. The @BeforeClass methods of superclasses would be run before those the current class.

Sometimes, some tests need to share expensive steps (such as database logons), which can break test independence and are usually optimized

annotating a public static void method with @beforeclass, and the method takes no arguments, causes the method to execute once before all test methods are executed and only once

The @beforeclass annotation method of the parent class executes before the @beforeclass annotation method of the subclass executes

@Ignore

Sometimes want to temporarily disable a test or a group of tests. Methods annotated with Test that is also annotated with @Ignore would not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing tests would be executed. Native JUnit 4 Test Runners should report the number of ignored tests along with the number of tests that ran and the numb Er of tests that failed.

Using a @ignore annotation on a class or @test annotation method that contains a test class will make the annotated class or method not be executed as a test

The number of tests that are ignored is reported in JUnit execution results

public class Mathtest {
	@Ignore ("does not test")
	@Test public
	void Testadd () {
		math m = new Math ();
		Asserttrue (M.add (1, 1) = = 2);}
}
@Ignore public
class Mathtest {
	@Test public
	void Testadd () {
		math m = new Math ();
		Asserttrue (M.add (1, 1) = = 2);}
}

The execution results are the same:

@Test

The test annotation tells JUnit, the public void method to which it was attached can be run as a Test case. To run the method, the JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test would be reported by JUnit as a failure. If No exceptions is thrown, the test was assumed to has succeeded.

The Test annotation supports and optional parameters.

The first, expected,declares that a test method should throw a exception. If it doesn ' t throw an exception or if it throws a different exception than the one declared, the test fails.

The Second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (me Asured in milliseconds).

The public void method for @Test annotations will be treated as a test case

JUnit creates a new test instance each time and then calls the @test annotation method

Any exception thrown will be considered a test failure

@Test annotations provide 2 parameters:

1, "expected", defines the exception that the test method should throw, and if the test method does not throw an exception or throws a different exception, the test fails

2, "Timeout", if the test runs longer than the defined time, the test fails (in milliseconds)

public class Mathtest {
	@Test (expected=exception.class) public
	void Testadd () throws exception{
		throw new Exception ();
	}
}
public class Mathtest {
	@Test (timeout=5000) public
	void Testadd () {for
		(;;) {
			
		}
	}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.