我越來越NPE
嘲笑一個嘲弄的方法。我的理解是,只要我們使用模擬對象調用方法,模擬就會處理所有其他事情。NullPointerException在使用Mockito調用模擬方法時
被測
public IDocumentSet sendDocuments(IClientUserDto cu, IDocumentSet ds) throws ESignatureProviderException {
logMethodStartDebug(cu, CLASS_NAME, "sendDocuments(IClientUserDto, IDocumentSet)");
IDocumentSet set = null;
List<DocumentContent> documentContent = new ArrayList<DocumentContent>();
Envelope env = new Envelope();
MultiPart multiPart = new MultiPart();
String token = null;
String envId = null;
String status = null;
try {
List<IDocumentDto> dtos = loadDocuments(cu, ds);
List<IDocument> docs = ds.getDocuments();
dtos = validateDocs(docs, dtos);
ISendDocumentsTransformerArgs args = new SendDocumentsTransformerArgs(cu);
args.setDocuments(docs);
args.setDocumentDtos(dtos);
args.setCallbackConfiguration(getConfiguration().getCallback());
args.setModuleConfiguration(getModuleConfiguration());
ITransformer transformer = new SendDocumentsRESTTransformer();
ITransformerResult result = transformer.transformRequest(args);
JUnit測試用例方法
@RunWith(PowerMockRunner.class)
@PrepareForTest({DtoUtils.class, ESignatureSpringUtil.class, AppContext.class, DocusignRESTUtil.class})
public class TestDocusignRESTProvider {
private String accountId = "025f1a5d-b796-4ba6-85d2-b2a4a90d109c";
private String address;
private IClientUserDto iClientUserDto;
private IClientUserVendorDto iClientUserVendorDto ;
private ILoggingHandler iloggingHandler;
private ApplicationContext applicationContext;
private DocusignRESTClient docusignRestClient;
private WebTarget webTarget;
private Response response;
private Invocation.Builder builder;
private IDocumentSet iDocumentSet;
private IDocumentManager iDocumentManager;
private IProviderConfiguration iProviderConfiguration;
private ITransformer iTransformer;
private ITransformerResult iTransformerResult;
private IManager iManager;
private SendDocumentsTransformerArgs args;
@Before
public void setUp() throws Exception {
iClientUserDto = new ClientUserDto();
iloggingHandler = mock(ILoggingHandler.class);
applicationContext = mock(ApplicationContext.class);
iClientUserVendorDto = mock(IClientUserVendorDto.class);
docusignRestClient = mock(DocusignRESTClient.class);
webTarget = mock(WebTarget.class);
response = mock(Response.class);
builder = mock(Invocation.Builder.class);
iDocumentSet = mock(IDocumentSet.class);
iDocumentManager = mock(IDocumentManager.class);
iProviderConfiguration = mock(IProviderConfiguration.class);
iTransformer = mock(ITransformer.class);
iTransformerResult = mock(ITransformerResult.class);
iManager = mock(IManager.class);
args = mock(SendDocumentsTransformerArgs.class);
PowerMockito.mockStatic(DtoUtils.class);
PowerMockito.mockStatic(ESignatureSpringUtil.class);
PowerMockito.mockStatic(AppContext.class);
PowerMockito.mockStatic(DocusignRESTUtil.class);
}
@Test
public void testSendDocuments() throws Exception {
AppContext.setApplicationContext(applicationContext);
IClientUserDto iClientUserDto = mock(IClientUserDto.class);
DocusignRESTProvider docusignRestProvider = new DocusignRESTProvider();
docusignRestProvider.setLoggingHandler(iloggingHandler);
docusignRestProvider.setDocumentManager(iDocumentManager);
docusignRestProvider.setConfiguration(iProviderConfiguration);
docusignRestProvider.setManager(iManager);
Mockito.when(iloggingHandler.isGeneralDebugEnabled()).thenReturn(Boolean.TRUE);
Mockito.when(iTransformer.transformRequest(args)).thenReturn(iTransformerResult);
docusignRestProvider.sendDocuments(iClientUserDto, iDocumentSet);
}
所以,當代碼到達transformer.transformRequest(args);
線它炸燬。我嘲笑ITransformer並調用了它的轉換請求方法,不應該返回給我一個模擬對象,或者我以錯誤的方式進行操作。
你能否提供堆棧跟蹤? –
昨天你問同樣的問題時,你讀過我最近的評論嗎?它解釋了你在這裏做錯了什麼。 –
@DavidWallace可能不是......而只是爲了記錄;請在這裏看看我的答案;並隨時提供一些反饋。正如我假設我正在解決他的問題背後的問題。 – GhostCat