JUnit is used for unit testing in the Java. Junit is included in family of unit testing frameworks all together known as xUnit, that originated with JUnit.Writing readable tests is also as important like writing readable production code.If you are developer you need to write test cases as writing the test cases is a very good practice.
For writing the test cases keep the points in mind :-
1). How the API can be access.
2). What data is supposed to go as an input and what we get as an output.
3). What possibility of variations of an expected behavior is exist.
4). What kind of exceptions is occur.
5). How individual parts of the system works with others.
The annotations that are used with the junit testing are as follows :-
1). @BeforeClass
2). @AfterClass
3). @Before
4). @After
5). @Test
6). @Setup
7). @Test (expected = Exception.class)
8). @Ignore
1). @BeforeClass :-
It runs only once just before the any method is executed in the test class.
2). @AfterClass :-
It runs only once just after all the method is executed in the test class.
3). @Before :-
It runs before the @Test method.
4). @After :-
It runs after the @Test method.
5). @Test :-
It is the annotation that says that this methos is to be tested.
6). @Setup :-
Under the setup you may initialliaze or setting up the mock enviroment that is require for the testing .
7). @Test (expected = Exception.class) :-
This is used when you know what exception is throw by your test case.
8). @Ignore :-
It is used to temporarily disabling the test.
Here is the sample test performing using the junit :-
public class MyClass { public void myTestMethod() { } public void myTestMethod2() { } }
Junit Test class :-
public class MyTestClass { @BeforeClass public void initGlobalResources() { /** * This method will be called only once per test class. */ } @Before public void initializeResources() { /** * This method will be called before calling every test. */ } @Test public void myTestMethod1() { } @Test public void myTestMethod2() { } }