当我使用超过1个ArgumentCaptor参数时,我收到一个空指针。有人知道是哪里出了问题吗?
@RunWith(PowerMockRunner.class)
@PrepareForTest(FileHelper.class)
public class MyTest {
...
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(FileHelper.class);
}
@Test
public void save() throws Exception {
ArgumentCaptor<String> argName = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<List> argList = ArgumentCaptor.forClass(List.class);
PowerMockito.doNothing().when(FileHelper.class, "saveTextFile", argName.capture(),
argList.capture());
...
}
FileHelper类中的saveTextFile方法:
public static void saveTextFile(String filename, List<String> data) {
...
}
如果我运行代码,我得到这个异常:
java.lang.NullPointerException. lang . class。isAssignableFrom(本地方法)org.powermock.reflect.internal.WhiteboxImpl.checkIfParameterTypesAreSame (WhiteboxImpl.java: 2432)org.powermock.reflect.internal.WhiteboxImpl.getMethods (WhiteboxImpl.java: 1934)org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate (WhiteboxImpl.java: 1025)org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException (WhiteboxImpl.java: 948)org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod (WhiteboxImpl.java: 882)org.powermock.reflect.internal.WhiteboxImpl.invokeMethod (WhiteboxImpl.java: 859)org.powermock.reflect.Whitebox.invokeMethod (Whitebox.java: 466)org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when (PowerMockitoStubberImpl.java: 106)
如果我调用一个只有一个参数的方法,即list参数一切正常
我想你可能想用"verify"而不是when。
您正在为mock设置"什么都不做"的期望。
调用被测试的方法,然后验证您期望发生的事情确实发生了,这不是更好吗?
例如,考虑这两个类…
HelperClass.java
public class HelperClass {
public static void nastyStaticCall(String filename, List<String> data) {
System.out.println("REAL METHOD CALLED!!");
}
}
MyClass.java
import java.util.Arrays;
public class MyClass {
public void testMethod() {
HelperClass.nastyStaticCall("FILENAME", Arrays.asList("Data"));
}
}
如果我想测试"MyClass",我会这样做…
import static org.fest.assertions.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.MockitoAnnotations;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(HelperClass.class)
public class MyClassTest {
private MyClass classUnderTest;
@Captor
private ArgumentCaptor<String> fileNameCaptor;
@Captor
private ArgumentCaptor<List<String>> dataCaptor;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockStatic(HelperClass.class);
classUnderTest = new MyClass();
}
@Test
public void shouldMakeNastyStaticCall() {
classUnderTest.testMethod();
verifyStatic();
HelperClass.nastyStaticCall(fileNameCaptor.capture(), dataCaptor.capture());
assertThat(fileNameCaptor.getValue()).isEqualTo("FILENAME");
assertThat(dataCaptor.getValue()).containsOnly("Data");
}
}
如您所见,这个测试方法所做的只是调用实际代码,然后验证是否使用正确的参数调用了helper类。
如果您实际期望静态方法调用返回一个值,该值将被您正在测试的方法的其余部分使用,那么您将需要使用"when",但由于它返回"void",因此您不需要做任何事情。
一般来说,我个人避免在"when"子句中使用参数捕获符,而将其留到"verify"阶段。