I have this following class A. It has two functions Barney and Ted. Barney calls Ted from inside. How to mock Ted's behavior in my test class?
package MockingNestedFunction;
import java.util.ArrayList;
import java.util.List;
public class A
{
public String Barney()
{
List<String> x =Ted();
String a="";
for(int i=0;i<x.size();i++)
{
a=a.concat(x.get(i));
}
return a;
}
public List<String> Ted()
{
List<String> x=new ArrayList<>();
x.add("A");
x.add("B");
x.add("C");
return x;
}
}
package MockingNestedFunction;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Mockito.when;
import static org.testng.Assert.*;
public class ATest
{
private A a;
@BeforeMethod
public void setup()
{
MockitoAnnotations.openMocks(this);
a=new A();
}
@Test
public void testA() throws Exception
{
List<String> x=new ArrayList<>();
x.add("D");
x.add("E");
x.add("F");
when(a.Ted()).thenReturn(x);
}
}
when(a.Ted()).thenReturn(x) returns the error,when() requires an argument which has to be 'a method call on a mock'. How to effectively mock this?