Avatar of gudii9
Flag for United States of America

asked on 

mockito example setter

I was writing below example

package com.gp.mockito;

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

Open in new window

package com.gp.mockito;

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

Open in new window

package com.gp.mockito;

public interface ICalculator {
public int add(int x,int y);

Open in new window

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?
Programming.NET ProgrammingJavaProgramming Languages-OtherProgramming Theory

Avatar of undefined
Last Comment

8/22/2022 - Mon