0% found this document useful (0 votes)
28 views

Mockito Example

The document discusses how to use Mockito to test Java classes. It provides an example of creating a Calculator class and test, and explains how Mockito allows testing the class without depending on real external services by creating mock objects.

Uploaded by

Sourabh Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Mockito Example

The document discusses how to use Mockito to test Java classes. It provides an example of creating a Calculator class and test, and explains how Mockito allows testing the class without depending on real external services by creating mock objects.

Uploaded by

Sourabh Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

First Download the Jar file from

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

So creating a calculator class like this:


package com.koenig.Demojunit;

public class Calculator {

public int add(int i, int j)


{
return i+j;
}
}

Now we will create a test case like this


In pom.xml replace the junit dependency with latest one form

https://mvnrepository.com/artifact/junit/junit/4.13.2

<!-- https://mvnrepository.com/artifact/junit/junit -->

<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 static org.junit.Assert.assertEquals;

import org.junit.Test;

public class TestCalculator {

Calculator c= new Calculator();

@Test
public void testAdd()
{
assertEquals(5, c.add(2, 3));

}
}

Now right click on the test class and run it as junit test

And you will get the ouput like this:

You can see the green flag in to the junit out put like above i.e test pass

But is we change the Calculator class like this:

package com.koenig.Demojunit;

public class Calculator {

public int add(int i, int j)


{
return i+j+1;
}
}
And then run the test class as junit test will get the out put as fail like below:

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.

Like in calculator class if we want to create the object of calculator first


before running the Testcase we can do it like this:

package com.koenig.Demojunit;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class TestCalculator {

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;

public interface CalculatorService {

public int add(int i, int j);


}

And here we can able to edit the calculator class like this:

package com.koenig.Demojunit;

public class Calculator {

CalculatorService service;

public int add(int i, int j)


{
return service.add(i, j);

}
}

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

That will make mockito famous here.


Now in the above Interface we need to test that the addition will be done by the service and the
result will be multiplied by i.

i.e (2, 3) = (i+j)*2

and it will add the two no here i.e i and j and result will multiply bu i.

So here we are going to change the Calculator class like this:


package com.koenig.Demojunit;

public class Calculator {

CalculatorService service;

public int perform(int i, int j) //(2,3) = (i+j)*2


{
return service.add(i, j)*i;

}
}

And will check by using this perform operation in test class I,e in TestCalculator.java class like this:
package com.koenig.Demojunit;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class TestCalculator {

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

So to use Mockito we need to add the dependency from maven ie:

https://mvnrepository.com/artifact/org.mockito/mockito-core/5.11.0

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-core</artifactId>

<version>5.11.0</version>

<scope>test</scope>

</dependency>

And Update the project.

In Calculator.java class edit it like this:


package com.koenig.Demojunit;

public class Calculator {

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 static org.junit.Assert.assertEquals;


import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class TestCalculator {

Calculator c= null;

CalculatorService service = Mockito.mock(CalculatorService.class);


@Before
public void setup()
{
c = new Calculator(service);
}

@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));

}
}

And will get the output like this:

You might also like