troubleshooting Question

mockito example setter

Avatar of gudii9
gudii9Flag for United States of America asked on
Programming.NET ProgrammingJavaProgramming Languages-OtherProgramming Theory
4 Comments1 Solution3020 ViewsLast Modified:
I was writing below example


import org.junit.Before;
import org.junit.Test;
import junit.framework.Assert;
import static org.mockito.Mockito.*;

public class CalculatorServiceTest {

	CalculatorService calService;
	public void setup(){
		ICalculator cal_mock = mock(ICalculator.class);
		when(cal_mock.add(2, 4)).thenReturn(6);
		calService=new CalculatorService();
	public void testAddNumbers(){
		Assert.assertEquals(6, calService.addTwoNumbers(2,4));

public class CalculatorService {
ICalculator cal;
public ICalculator getCal() {
	return cal;
public void setCal(ICalculator cal) { = cal;
public int addTwoNumbers(int x, int y){
	return cal.add(x, y);

public interface ICalculator {
public int add(int x,int y);
why we need setter for calservice implementation class.

why are they setting interface mock object to the calservice object

observerd one example here

i do not see any setter here though?
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros