Mockito Example
Mockito Example
https://mvnrepository.com/artifact/org.mockito/mockito-all/2.0.2-beta
Before going into the details of the Mockito Framework, let's see
an application in action. In this example, we've created a mock of
Stock Service to get the dummy price of some stocks and unit
tested a java class named Portfolio.
Why we need testing or junit frame work
When we make a project we need lots of classes , and to build an
application we need a method and we need to test that method
also.
So how to test that method, one way is to build the complete
application and then test the method but it is not that flexible to
test the entire application an , i.e if something goes wrong , how
would you know what went wrong and where.
So we test the individual unit the classes and method and there
method and by joint we can do it.
And when we expand the maven project by default the junit
dependency was there.
Now we have to implement that here by tdd that is we can create a test case and then we will create
the module.
But it is very lengthy, so here we are using simple creating method first and the create the test for it
https://mvnrepository.com/artifact/junit/junit/4.13.2
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
And write the following code in TestCalculator class like this:
package com.koenig.Demojunit;
import org.junit.Test;
@Test
public void testAdd()
{
assertEquals(5, c.add(2, 3));
}
}
Now right click on the test class and run it as junit test
You can see the green flag in to the junit out put like above i.e test pass
package com.koenig.Demojunit;
Now there are various method of Junit we can use here as like
@Before to run a particular method before to run a Test case
@After to run a method after running the test case.
package com.koenig.Demojunit;
import org.junit.Before;
import org.junit.Test;
Calculator c= null;
@Before
public void setup()
{
c = new Calculator();
}
@Test
public void testAdd()
{
assertEquals(5, c.add(2, 3));
}
}
Need of the mockito?
This is just the explanation here:
So let’s we have interface name CalculatorService
Like this:
package com.koenig.Demojunit;
And here we can able to edit the calculator class like this:
package com.koenig.Demojunit;
CalculatorService service;
}
}
And we have can imagine that the interface we have created in somewhere on the cloud as a service
we are using in our application and it is working correctly or we can say it is a service that will use
some database to work.
And we know that the service I already good we don’t need to test the service here,
We need to test that how our Calculator class is working or the method at our side is working fine
with the database or not.
So for that we can use the stub instead of live service which will work as a service class which is on
the cloud and we can test our class and method with out depending and using the resources.
Even the some other class object i.e that service class object is not build over here and still we can
able to test our code and that’s where we have to mock those services and when I say mock we are
creating the duplicate service here..
and it will add the two no here i.e i and j and result will multiply bu i.
CalculatorService service;
}
}
And will check by using this perform operation in test class I,e in TestCalculator.java class like this:
package com.koenig.Demojunit;
import org.junit.Before;
import org.junit.Test;
Calculator c= null;
@Before
public void setup()
{
c = new Calculator();
}
@Test
public void testAdd()
{
assertEquals(10, c.perform(2, 3));
}
}
Here it will check for 10 and when we are going to run this or test this application and run this
TestCalculator class as a test class will get the output as failure like this:
So why we are getting this error here because to make it working we need the object of the
CalculatorService Interface. And we will get it like this:
But here we don’t want to use the live service or use our resources so for that we have to create a
fake object of CalculatorService. And that we can do by using the Stub here
https://mvnrepository.com/artifact/org.mockito/mockito-core/5.11.0
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
CalculatorService service;
public Calculator(CalculatorService service) {
this.service = service;
}
public int perform(int i, int j) //(2,3) = (i+j)*2
{
return service.add(i, j)*i;
//return (i+j)*i; //this is to verify
}
}
Also Edit the TestCalculator.java Class Like this:
package com.koenig.Demojunit;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Calculator c= null;
@Test
public void testAdd()
{
when(service.add(2, 3)).thenReturn(5);// this is not the testing
method so thats why giving this static values
assertEquals(10, c.perform(2, 3));//this what we have to test
verify(service.add(2,3));
}
}