Tuesday, September 3, 2013

Junit Basic ( Fixtures / Annotation / Assert )


Junit Basic ( Fixtures / Annotation / Assert )

Fixtures -
 
Fixtures is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. It includes ;

setUp() - method which runs before every test invocation.
tearDown() - method which runs after every test method.

e.g. : @Before, @After, @BeforeClass, @AfterClass


JUnit Annotations

@Test : The annotation @Test identifies that a method is a test method.

@Before : This method is executed before each test.
                    This method can prepare the test environment
                         e.g. read input data, initialize the class

@After : This method is executed after each test.
                 This method can cleanup the test environment.
                 It can also save memory by cleaning up expensive memory structures.
                        e.g. delete temporary data, restore defaults.

@BeforeClass : This method is executed once, before the start of all tests.
                 This can be used to perform time intensive activities, for example to connect to a  database.  Methods annotated with this annotation need to be defined as static to work with JUnit.

@AfterClass : This method is executed once, after all tests have been finished.
                 This can be 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.

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


Assert - A set of assert methods.

TestCase - A test case defines the fixture to run multiple tests.

TestResult - A TestResult collects the results of executing a test case.

TestSuite - A TestSuite is a Composite of Tests.


Assert statements
JUnit provides static methods in the Assert class to test for certain conditions.

These assertion methods typically start with asserts and allow you to specify the error message, the expected and the actual result.

An assertion method compares the actual value returned by a test to the expected value, and throws an AssertionException if the comparison test fails.

fail(String) :
       Let the method fail.
       Might be used to check that a certain part of the code is not reached.
       Or to have a failing test before the test code is implemented.

assertTrue([message], boolean condition) :
       Checks that the boolean condition is true.

assertsEquals([String message], expected, actual) :
       Tests that two values are the same.
       Note: for arrays the reference is checked not the content of the arrays.

assertsEquals([String message], expected, actual, tolerance) :
       Test that float or double values match.
       The tolerance is the number of decimals which must be the same.

assertNull([message], object) :
       Checks that the object is null.

assertNotNull([message], object) :
       Checks that the object is not null.

assertSame([String], expected, actual) :
       Checks that both variables refer to the same object.

assertNotSame([String], expected, actual) :
       Checks that both variables refer to different objects.