JUnit Tutorial

Junit:-
it automate test case process:--
It is one of the unit testing framework. Current version is junit 4.
Annotations for Junit testing
The Junit 4.x framework is annotation based, so let's see the annotations that can be used while writing the test cases.

@Test annotation specifies that method is the test method.

@Test(timeout=1000) annotation specifies that method will be failed if it takes longer than 1000 milliseconds (1 second).

@BeforeClass annotation specifies that method will be invoked only once, before starting all the tests.

@Before annotation specifies that method will be invoked before each test.

@After annotation specifies that method will be invoked after each test.

@AfterClass annotation specifies that method will be invoked only once, after finishing all the tests.
--------------------
Assert class

The org.junit.Assert class provides methods to assert the program logic.

Methods of Assert class

The common methods of Assert class are as follows:

void assertEquals(boolean expected,boolean actual): checks that two primitives/objects are equal. It is overloaded.
void assertTrue(boolean condition): checks that a condition is true.
void assertFalse(boolean condition): checks that a condition is false.
void assertNull(Object obj): checks that object is null.
void assertNotNull(Object obj): checks that object is not null.
---------
Still we have lot of methods of assertEquals() with various of parameters.
Eclipse it's easy process just create a class with various of it's method. and the click of that
class correspond  to that crate a junit test case with clicking of it's all method. for those you want test case
class will create with all annotation. now you need to call that logic. based on Assert class methods
like assertequal();... etc
assertEquals(4,Calculation.findMax(new int[]{1,3,4,2}));  

it will return you the value if true then no of cases will be green and pass else red means wrong
-----------
TestSuite:
--
Instead of keeping all test case one by one just create the test suite with all the test case you created it will check and ..
give you result like 20 case 10 pass 10 failed
it's good for business purpose where logic is written in 300 lines
we can check all lins just simple will check inside Junit that ..working or not
-------
it's enough for junit. all the best.
link:-
http://www.javatpoint.com/junit-tutorial
https://www.youtube.com/watch?v=GYtt29ZoNw8
http://javarevisited.blogspot.in/2012/06/junit4-annotations-test-examples-and.html
JUnit is the most famous unit test framework in Java. However it is only suitable for pure unit testing, for integration test, you should go for TestNG instead.

JUnit 4.x: EX--

No comments:

Post a Comment