Posts

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

Source: guru99.com JUnit assertEquals You have  assertEquals(a,b)  which relies on the  equals()  method of the Object class. Here it will be evaluated as  a.equals( b ). Here the class under test is used to determine a suitable equality relation. If a class does not override the  equals()  method of  Object  class, it   will get the default behaviour of  equals()  method, i.e. object identity. If  a  and  b  are primitives such as  byte ,  int ,  boolean , etc. then the following will be done for assertEquals(a,b) : a  and  b  will be converted to their equivalent wrapper object type ( Byte,Integer ,  Boolean , etc.), and then  a.equals( b )  will be evaluated. For Example: Consider below-mentioned strings having same values, let's test it using assertTrue String obj1="Junit"; String obj2="Junit"; assertEquals(obj1,obj2); Above assert stateme...

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

Create Junit Test cases

Main Class: package mavenProjectJunitFrameworkTestcase.MavenProjectJunitFrameworkTestcase; /**  * Hello world!  *  */ public class App { String name;     public static void main( String[] args )     {         System.out.println( "Hello World!" );     }       //setName method used to set some value to class variable     public String setName(String name) {     return this.name=name;     }       //sum method is used to calculate sum of two variables     public int sum(int a, int b) {     int c = a+b;     return c;     } } Junit Test for Apptest class; //Generally we junit class with the word 'test' appended to the name of mainclass which we are about to test  package mavenProjectJunitFrameworkTestcase.MavenProjectJunitFrameworkTestcase; import static org.junit.Assert.*; ...

Junit Mockito example

//Mainclass: App //We will mock the setName method in App class using Mockito package mavenProjectJunitFrameworkTestcase.MavenProjectJunitFrameworkTestcase; public class App { String name;     public static void main( String[] args )     {         System.out.println( "Hello World!" );     }         //setName method used to set some value to class variable     public String setName (String name) {     return this.name=name;     }         //sum method is used to calculate sum of two variables     public int sum(int a, int b) {     int c = a+b;     return c;     } } Junit AppTest class: package mavenProjectJunitFrameworkTestcase.MavenProjectJunitFrameworkTestcase; import static org.junit.Assert.*; import org.junit.Test; import org.mockito.Mockito; import junit.framework.Asse...

Junit with Mockito. Nothing but mocking the responses of some methods using Mockito.

Image
How to test by mocking the response of methods of third party systems.