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;
@Before
public void setup(){
ICalculator cal_mock = mock(ICalculator.class);
when(cal_mock.add(2, 4)).thenReturn(6);
calService=new CalculatorService();
//calService.setCal(cal_mock);
}
@Test
public void testAddNumbers(){
Assert.assertEquals(6, calService.addTwoNumbers(2,4));
}
}
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);
}
}
package com.gp.mockito;
public interface ICalculator {
public int add(int x,int y);
}
why we need setter for calservice implementation class.Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”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.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.