2013-01-22 87 views
6

我試圖用mockito/powermock來嘲諷私有方法。我得到的NullPointerException
我試圖做一個簡單的例子是:當試圖用mockito和powermock模擬私有方法時,獲取java.lang.NullPointerException

實際的類

import com.siriusforce.plugin.common.PluginSystem; 
import com.wellsfargo.core.business.service.dp.fulfillment.MockitoBusinessService; 

public class MockitoBusinessOperationImpl implements MockitoBusinessOperation{ 
    private MockitoBusinessService mockitoBusinessService = PluginSystem.INSTANCE.getPluginInjector().getInstance(MockitoBusinessService.class); 
    private Long Id; 

    public String creditAproved(Long Id){ 
     System.out.println("Came Inside MockitoBusinessOperationImpl"); 
     this.Id = Id; 
     if (this.Id != null){ 
      System.out.println("Inside creditaproved If statement"); 
      String Report = mockitoBusinessService.creditReport(this.Id); 
      System.out.println("Mock Object Injected from test class "+ Report); 
      return Report; 
     } else 
      return "Went to Else Part"; 
    } 

    private String inTestMethod(Long Id){ 
     return "this is working"; 
    } 
} 

測試類:

import static org.mockito.Mockito.spy; 
import static org.mockito.Mockito.when; 
import static org.powermock.api.mockito.PowerMockito.doReturn; 

import org.mockito.InjectMocks; 
import org.mockito.Mock; 
import org.mockito.MockitoAnnotations; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.testng.annotations.BeforeMethod; 
import org.testng.annotations.Test; 

import com.siriusforce.plugin.common.PluginSystem; 

public class MockitoBusinessServiceTest { 

    @Mock 
    MockitoBusinessService MockitoBusinessService ; 

    @InjectMocks  
    private MockitoBusinessOperation MockitoBusinessOperation = PluginSystem.INSTANCE.getPluginInjector().getInstance(MockitoBusinessOperation.class); 

    private Long Id; 

    @BeforeMethod 
    public void init() { 
     MockitoAnnotations.initMocks(this); 
     this.Id = 123L; 
    } 

    @PrepareForTest(MockitoBusinessOperation.class) 
    @Test(enabled = true) 
    public void testReCalculatePrepaids() throws Exception { 
     MockitoBusinessOperation = spy(MockitoBusinessOperation); 
     doReturn("this will work hopefully").when(MockitoBusinessOperation, "inTestMethod", this.Id); 

     when(MockitoBusinessService.creditReport(this.Id)).thenReturn(new String("Decline by only Me")); 

     String mainReport = MockitoBusinessOperation.creditAproved(this.Id); 
     System.out.println("Indirect Call from actual class MainReport " + mainReport); 

    } 
} 

當我嘗試運行此我得到一個NullPointerException:任何解決這個或任何其他方式嘲笑私人方法的建議。我不想使用給出的方法來保護這個方法,這意味着改變實際的方法。我不想你必須使用PowerMockito.spy()代替Mockito.spy()任何改變實際

class method 
    <failure type="java.lang.NullPointerException">java.lang.NullPointerException 

at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing(PowerMockitoStubberImpl.java:68) 

at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.prepareForStubbing(PowerMockitoStubberImpl.java:123) 

at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:91) 

at com.wellsfargo.core.business.service.dp.fulfillment.MockitoBusinessServiceTest.testReCalculatePrepaids(MockitoBusinessServiceTest.java:54) 

+1

我找出NullPointerException異常:只需添加PowerMockito的間諜方法。 'MockitoBusinessOperation = PowerMockito.spy(MockitoBusinessOperation);'現在問題在於嘲笑私有方法,儘管我嘲笑了私有方法,但它仍然在調用。 [在另一篇文章](http://stackoverflow.com/questions/8057607/when-mocking-private-method-with-powermock-but-underlying-method-still-gets-cal?rq=1)有人發佈了此問題。但解決方案並不適合我。任何建議爲什麼模擬沒有得到反映,因爲它假設是。 – jan

+0

你在使用doWhen()風格的嘲諷嗎?你能發佈一個新的代碼示例或修改你的問題嗎? –

回答

11

嘗試導入static org.powermock.api.mockito.PowerMockito.spy;

相關問題