2011-02-04 7 views
1

Mockeryを使ってjavaでFileオブジェクトをモックできるようにしたい。私は、javaのFileのインターフェースを作成できないようです。これは可能ですか?Mockery、ファイルシステムオブジェクトを模倣する

EDIT:

私はインデクサークラスにindexDoc機能をテストする必要があります。インデクサクラス

private static void indexDocs(File file) throws Exception{ 
    //Check for file to be a directory or file to be indexed look for html files and add to document  
    if(file.isDirectory()){ 
    String[] files = file.list(); 
    Arrays.sort(files); 
    for (int i = 0; i < files.length; i++) // recursively index them 
     indexDocs(new File(file, files[i])); 
    } else if(file.getPath().endsWith(".html") || file.getPath().endsWith("htm")){ 
    // Get the document from HTMLDocument class which takes care of stripping of HTML tag, get the path 
    // of HTML file and title of HTML document. 
    Document doc = HTMLDocument.Document(file); 

    // TODO Get the book of HTML, it can be a part of HTML document class. 
    writer.addDocument(doc); 
    } 
} 

答えて

2

ファイルシステムをモックしないでください。私たちは初期の段階でこれをやろうとしましたが、設計をガイドするためのテストの使用を迂回しました。

あなたのコードを簡単に見ていくと、2つのことが起こります.1つはファイルナビゲーションであり、もう1つはhtmlストリッピングです。おそらく1つの選択肢は、htmlストライピングオブジェクト(共同作業者として渡される)を導入して模擬し、実際のファイルシステムの例に対してテストを書くことです。

1

Jmockで

@Test 
public void testindexDocs() 
{  
    final File f = mockFile.mock(File.class); 
    File file = new File("test");  
    mockFile.setImposteriser(ClassImposteriser.INSTANCE);  
    final String[] files = { 
     "C:\\test\\", 
     "C:\\test\\test1.html", 
     "C:\\test\\test2", 
     "C:\\test\\test3.html"};   
    mockFile.checking(new Expectations(){ 
    { 
     one(f).list();will(returnValue(files)); 
    } 
    });  
    //TODO test if list() how many time i have called 
    //Document doc = HTMLDocument.Document(file); in function indexDocs 
} 

インデックスドキュメント機能は、具象クラスをモックすることができます。ちょうど

Mockery context = new Mockery(); 

context.setImposteriser(ClassImposteriser.INSTANCE); 
+0

私は質問を編集しました。あなたは私の懸念を少しは良く理解しています。 – remo

0

正確な理由が問題であるため、具体的なクラスではなく抽象を使用する必要があります。