Saturday, March 1, 2014

Simple introduction to JUnit

Hi All,

  After a long time I am started posting ....:)

  In this post I show you how to use JUnit4.11

Junit is a java framework for the sake of unit test for our java code.

Here we have to know some notable annnotations .

1) @Before
2) @After
3) @BeforeClass
4) @AfterClass
5) @Test

@Before
    This annotation is used for any object level initialisation part.

This initialisation part will execute once per each test case in a test class.

Suppose if a test class has 4 test cases then this initialisation part will execute 4 times.

@After
    This annotation is used for any object level cleanup part.

This cleanup part will execute once per each test case in a test class.

Suppose if a test class has 4 test cases then this cleanup part will execute 4 times.

@BeforeClass
    This annotation is used for class level initialisation part.

This initialisation part will execute once per test class.

Suppose if a test class has 4 test cases then this cleanup part will execute 1 time only.

@AfterClass
    This annotation is used for class level cleanup part.

This cleanup part will execute once per test class.

Suppose if a test class has 4 test cases then this cleanup part will execute 1 time only.

@Test
    This annotation is the main one which executes our test case.

If you want any method to be executed without main method then use this annotation.

You have to use assertions for test pass.
We can have many assertions from junit framework.

Ex:- assertNotNull,
    assertNull,
    assertTrue,
    assertFalse,
    assertEquals,
    assertNotEquals.....etc