我所处的场景
public class SecondClass{
SecondClass(FirstClass fc){
...
}
public void foo(String a,String b){
....
}
}
public class FirstClass{
private SecondClass sc;
public void init(){
sc = new SecondClass(this);
}
public void bar(List<Integer> input){
.....
sc.foo(s1,s2);
}
}
我想掌握进入 foo 的字符串参数 a 和 b。测试类如下所示
@PrepareForTest({ FirstClass.class, SecondClass.class })
public class SampleTest
{
private String[] texts;
@Test
public void testBar() throws Exception
{
texts = new String[2];
final FirstClass fc = mock(FirstClass.class);
final SecondClass sc = spy(new SecondClass(fc));
doAnswer(invocation -> {
texts = (String[]) invocation.getArguments();
return null;
}).when(sc).foo(anyString(), anyString());
final List<Integer> input = new ArrayList<>();
input.add(1);
fc.bar(input);
System.out.println(texts[0]+"<>"+text[1]);
}
}
最后的 sysout 打印 null<>null。为什么 texts 数组没有更新?