Notes on Junit


  • Junit class is nothing but a normal class but with no main() method
  • Junit class will get executed test by test based on @Test annotations.
  • Actually we generally use Junit framework to test methods in Main class (or some class)
  • Each method in main class is treated as test scenario in Junit class. So test scenarios are actual methods in Junit class. Each method in Junit is associated with method in Main Class, thats how test cases are written.
  • We write test cases using Assert.assertEquals 
  • Actually Junit class has Test methods of actual methods of someother class which we are testing in Junit class
  • Each method in main class will generally have test method in junit class with annotation @Test on top of these test methods
  • Each test methods will have test cases of specific method which are testing and test cases are generally validated with Assert.assertEquals(Expected, Actual)
  • Generally we create Junit class on someother class, so that Junit class will have test cases(@Test) associated individual methods
  • Mockito is one of the library through which we can mock the response of methods using methods 'mock' and 'when' of Mockito
  • under @Test method, we use method 'mock' to tell junit that which class we are going to mock. And method 'when' will tell junit that to return some mocked value when main class method is called. 'verify' method in mockito will tell whether mocked method is called or not.
  • Ex: 
@Test
public void sumTest(int a, int b){
MainClass obj = Mockito.mock(MainClass.class);
Mockito.when(obj.sum(int a, int b)).thenReturn(5);
Mockito.verify(obj).sum(int a.int b);

Comments

Popular posts from this blog

Use 'Assert.assertEquals' to test method using Junit framework

Junit Mockito example