2013-01-22 33 views
6

私はmockito/powermockでプライベートメソッドをモックしようとしています。私はこれを実行しようとすると、私は取得mockitoとpowermockでプライベートメソッドをモックしようとしたときにjava.lang.NullPointerExceptionを取得する

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); 

    } 
} 

実際のクラス

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"; 
    } 
} 

テストクラスを:私NullPointerExceptionが発生
私がやろうとしています簡単な例があるに取得します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

をインポートしてください。 '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

に変更をしたくありません!ちょうどスパイ法にPowerMockitoを追加することによって:

は私がNullPointerExceptionが把握static org.powermock.api.mockito.PowerMockito.spy;

関連する問題