@Test
public void method()
public void method()
2. This method is executed before each Test:
Note: used to prepare the test environment (i.e. Read input files..)
@Before
public void method()
3. This method is executed after each Test:
Note: To cleanup environment (i.e. Delete temporary file, data..)
@After
public void method()
4.This method is executed ONLY once before start of All tests.
Note: To perform time intensive activities (i.e. DB connections..)
@BeforeClass
public static void method()
5. This method is executed ONLY once after all tests.
Note: i.e. To disconnect DB
@AfterClass
public static void method()
6. To ignore one test method:
Note: Do not execute this method
@Ignore
some_method()
7, Fail the test if the method takes longer than n milliseconds:
Note: Fails the test if it takes > 100 milliseconds
@Test(timeout=100)
some_method()
8. Fail the test if method NOT throwing the named exception;
Note; Test has to throw out the specified exception.
@Test(expected=Some_Exception.class)
some_method()
9. Hamcrest
assertThat( actual, is(expected value));
Hamcrest comes with a library of useful matchers. Here are some of the most important ones.
- Core
- anything - always matches, useful if you don't care what the object under test is
- describedAs - decorator to adding custom failure description
- is - decorator to improve readability - see "Sugar", below
- Logical
- allOf - matches if all matchers match, short circuits (like Java &&)
- anyOf - matches if any matchers match, short circuits (like Java ||)
- not - matches if the wrapped matcher doesn't match and vice versa
- Object
- equalTo - test object equality using Object.equals
- hasToString - test Object.toString
- instanceOf, isCompatibleType - test type
- notNullValue, nullValue - test for null
- sameInstance - test object identity
- Beans
- hasProperty - test JavaBeans properties
- Collections
- array - test an array's elements against an array of matchers
- hasEntry, hasKey, hasValue - test a map contains an entry, key or value
- hasItem, hasItems - test a collection contains elements
- hasItemInArray - test an array contains an element
- Number
- closeTo - test floating point values are close to a given value
- greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo - test ordering
- Text
- equalToIgnoringCase - test string equality ignoring case
- equalToIgnoringWhiteSpace - test string equality ignoring differences in runs of whitespace
- containsString, endsWith, startsWith - test string matching