2017-11-16 14 views
-1

ここでは非常に奇妙な問題に直面しています。私のテストクラスが続きPowerMockito:コードが別のクラスにある場合、コンストラクタをモックできません

SearchViewModel.java

@RunWith(PowerMockRunner.class) 
@PrepareForTest({JSONReader.class, ConstantsPath.class, DatabaseManager.class}) 
public class SearchViewModelTest { 

    @Rule 
    public TestRule rule = new InstantTaskExecutorRule(); 
    private String indexSearchContent; 
    private String fullTextSearchContentGL1, fullTextSearchContentGL2, fullTextSearchContentGL3, fullTextSearchContentGL4; 
    private String searchQuery = "a"; 
    private List<FullTextSearchItem> fullTextSearchResult; 
    private String behaviorString; 
    private SearchViewModel searchViewModel; 
    private DatabaseManager databaseManager; 

    private void initInputs() throws IOException { 
     indexSearchContent = JSONReader.convertStreamToString(getClass().getClassLoader().getResourceAsStream(ConstantsPath.getInstance().getIndexSearchFilePath())); 
     behaviorString = JSONReader.convertStreamToString(getClass().getClassLoader().getResourceAsStream(ConstantsPath.getInstance().getBehavioralFilePath())); 

     fullTextSearchContentGL1 = JSONReader.convertStreamToString(getClass().getClassLoader().getResourceAsStream(ConstantsPath.getInstance().getFullTextSearchFilePath("1"))); 
     fullTextSearchContentGL2 = JSONReader.convertStreamToString(getClass().getClassLoader().getResourceAsStream(ConstantsPath.getInstance().getFullTextSearchFilePath("2"))); 
     fullTextSearchContentGL3 = JSONReader.convertStreamToString(getClass().getClassLoader().getResourceAsStream(ConstantsPath.getInstance().getFullTextSearchFilePath("3"))); 


    } 

    private void mockDaggerDependency() { 
     AppInfo appInfo = Mockito.mock(AppInfo.class); 
     Mockito.when(appInfo.getAppName()).thenReturn("testApp"); 
     Mockito.when(appInfo.getAppLanguage()).thenReturn("EN"); 

     TestApplicationModule module = new TestApplicationModule(appInfo); 
     DatabaseModule databaseModule = Mockito.mock(DatabaseModule.class); 
     Component component = DaggerComponent.builder().applicationModule(module).databaseModule(databaseModule).build(); 
     MyApplication.setComponent(component); 
    } 

    private void mockGuidelineList() throws Exception { 
     databaseManager = PowerMockito.mock(DatabaseManager.class); 
     List<Guideline> mockedGls = new ArrayList<>(); 
     Guideline gl = new Guideline(); 
     gl.setGuidelineId("1"); 
     mockedGls.add(gl); 
     gl = new Guideline(); 
     gl.setGuidelineId("2"); 
     mockedGls.add(gl); 
     gl = new Guideline(); 
     gl.setGuidelineId("3"); 
     mockedGls.add(gl); 
     Mockito.when(databaseManager.getGuidelinesListByPositionOnHome()).thenReturn(mockedGls); 

     PowerMockito.whenNew(DatabaseManager.class).withNoArguments().thenReturn(databaseManager); 
     // prepare expected output for fulltext search 
     Observable.fromIterable(new DatabaseManager().getGuidelinesListByPositionOnHome()) 
       .map(Guideline::getGuidelineId) 
       .flatMap(glId -> BehavioralFile.<List<FullTextSearchItem>>loadJsonFile(ConstantsPath.getInstance().getFullTextSearchFilePath(glId), 
         new TypeToken<List<FullTextSearchItem>>() { 
         }.getType()).toObservable() 
         .flatMapIterable(fullTextSearchitems -> fullTextSearchitems) 
         .filter(item -> item.getText().toLowerCase().contains(searchQuery.toLowerCase()))).<List<FullTextSearchItem>>toList() 
       .subscribe(list -> { 
        fullTextSearchResult = list; 
       }); 
    } 

    @Before 
    public void setUp() throws Exception { 
     MainActivityTest.overrideRxJavaPlugins(); 
     mockDaggerDependency(); 
     initInputs(); 

     PowerMockito.mockStatic(JSONReader.class); 
     BDDMockito.given(JSONReader.readJsonFile(ConstantsPath.getInstance().getIndexSearchFilePath())).willReturn(indexSearchContent); 

     BDDMockito.given(JSONReader.readJsonFile(ConstantsPath.getInstance().getFullTextSearchFilePath("1"))).willReturn(fullTextSearchContentGL1); 
     BDDMockito.given(JSONReader.readJsonFile(ConstantsPath.getInstance().getFullTextSearchFilePath("2"))).willReturn(fullTextSearchContentGL2); 
     BDDMockito.given(JSONReader.readJsonFile(ConstantsPath.getInstance().getFullTextSearchFilePath("3"))).willReturn(fullTextSearchContentGL3); 

     BDDMockito.given(JSONReader.readJsonFile(ConstantsPath.getInstance().getBehavioralFilePath())).willReturn(behaviorString); 

     mockGuidelineList(); 

     searchViewModel = new SearchViewModel(); 
    } 

    @After 
    public void tearDown() throws Exception { 
    } 

    @Test 
    public void loadFullTextSearch() throws Exception { 

     //searchViewModel.loadFullTextSearch_(searchQuery); 
     loadFullTextSearch(searchQuery); 

     assertEquals(searchViewModel.fullTextSearchListLiveData.getValue().size(), fullTextSearchResult.size()); 
    } 

    private void loadFullTextSearch(String query) { 
     // following line is throwing exception if put in another class. 
     Observable.fromIterable(new DatabaseManager().getGuidelinesListByPositionOnHome()) 
       .map(Guideline::getGuidelineId) 
       .subscribeOn(AndroidSchedulers.mainThread()) 
       .observeOn(Schedulers.io()) 
       .flatMap(glId -> BehavioralFile.<List<FullTextSearchItem>>loadJsonFile(ConstantsPath.getInstance().getFullTextSearchFilePath(glId), 
         new TypeToken<List<FullTextSearchItem>>() { 
         }.getType()).toObservable() 
         .flatMapIterable(fullTextSearchitems -> fullTextSearchitems) 
         .filter(item -> item.getText().toLowerCase().contains(query.toLowerCase()))).<List<FullTextSearchItem>>toList().toObservable() 
       .subscribe(list -> searchViewModel.fullTextSearchListLiveData.setValue(list)); 
    } 

} 

ここloadFullTextSearch()テストケースiのラインsearchViewModel.loadFullTextSearch_(searchQuery);のコメントを削除するまで完全に正常に動作し、

チェック修正テストケース:

@Test 
public void loadFullTextSearch() throws Exception { 

     searchViewModel.loadFullTextSearch_(searchQuery); 
     //loadFullTextSearch(searchQuery); 

     assertEquals(searchViewModel.fullTextSearchListLiveData.getValue().size(), fullTextSearchResult.size()); 
    } 

earchViewModel.loadFullTextSearch_(),)は同じコードを持っていますが、loadFullTestSearch_()はSearchViewModelクラスにあり、loadFullTextSearch()は自分自身のテストケースです。なぜ、DatabaseManagerクラスのコンストラクタが偽装されていないのかを把握しました。コードがSearchViewModelクラスの場合はObservable.fromIterable(new DatabaseManager().getGuidelinesListByPositionOnHome()))。

注:私はDatabaseManagerクラスのコンストラクタをモックしています。 mockGuidelineList()メソッドをチェックしてください。コンストラクタが同じテストクラスで呼び出されている場合、Mockedメソッドは動作しています。私が手

例外は次のとおりです。

java.lang.NullPointerException: Cannot return null from a [email protected] @Provides method 

私は短剣と私は、データベース・オブジェクトの依存関係を初期化するからかっていコンストラクタを使用していますので。

public DatabaseManager() { 
    MyApplication.getComponent().inject(this); 
} 

すべてのヘルプは、おかげでいただければ幸いです。

答えて

0

私はすべてうまくやっていましたが、コンストラクタが呼び出されているクラスを追加するのには、@PrepareForTestアノテーションでは欠けていました。

だから私の場合には、これがなければならない:

@PrepareForTest({JSONReader.class, ConstantsPath.class, DatabaseManager.class, SearchViewModel.class}) 

this stackoverflowの答えは私を助けました。

関連する問題