2013-04-05 59 views
-1

我有一個web應用程序項目,我試圖單元測試使用FreeMarker模板創建文件的方法。我的方法createFile()應該採用MyFile類型 - 包含要創建的文件名以及RootMap FreeMarker需要的和模板名稱 - 並使用我提供的模板創建一個文件。如何在單元測試期間訪問.war類路徑中的文件?

我正在關注Freemarker manual以設置模板加載器。問題是,我使用TemplateLoader setClassForTemplateLoading(Class,String)方法來查找模板路徑。這個模板加載器使用Class.getResource()來獲取類路徑。但是,由於我使用Maven,因此我在/ src/main/java中使用了我的java代碼,在/ src/main/webapp/templates /中使用了我的模板,在/ src/test/java中使用了我的測試代碼。因此,我的Class.getResource(「/」)(根類路徑)始終返回<PATH_TO_PROJECT>/target/test-classes/

由於我將部署戰爭,因此我無法使用setDirectoryForTemplateLoading(File)。另外,由於我正在測試我的應用程序,因此我沒有ServletContext與setServletContextForTemplateLoading(Object,String)一起使用。

如何從測試用例訪問我的模板文件夾?

這裏是我的測試代碼(我用的Mockito嘲笑MyFile的類的行爲)一個簡單的例子:

private MyFile myFile; 
private FileGenerator fileGenerator; 

@Before 
public void setUp() { 
    myFile = new MyFile(...); 
    fileGenerator = new FileGenerator(myFile, ...); 
} 

@Test 
public void shouldCreateFile() { 
    final MyFile mockedMyFile = spy(file); 
    final Map<String, Object> rootMap = new HashMap<String, Object>(); 

    // populates rootMap with stuff needed for the Template 

    // mocking method return 
    when(mockedMyFile.getRootMap()).thenReturn(rootMap); 

    // replacing the MyFile implementation with my Mock 
    fileGenerator.setMyFile(mockedMyFile); 

    // calling the method I want to test 
    fileGenerator.createFile(); 

    assertTrue(MyFile.getFile().exists()); 
} 

這裏是代碼的簡化我的測試:

public void createFile() { 
    final Configuration cfg = new Configuration(); 
    cfg.setClassForTemplateLoading(getClass(), "templates/"); 
    try { 
     myFile.getFile().createNewFile(); 
     final Template template = cfg.getTemplate("template.ftl"); 
     final Writer writer = new FileWriter(myFile.getFile()); 
     template.process(myFile.getRootMap(), writer); 
     writer.flush(); 
     writer.close(); 
    } 
    // exception handling 
} 
+1

因爲您已經在使用mockito,所以您可以嘗試使用powermock來模擬靜態/最終/私有方法,例如Class.getResource() – Morfic 2013-04-05 16:32:33

+1

確實使用**/src/main/test **作爲測試代碼,而不是* * src/test/java **? – khmarbaise 2013-04-05 17:20:54

+0

@格羅夫我會試試!謝謝 – Tarek 2013-04-05 17:25:04

回答

0

我申請了Charles Forsythe的建議,結果很好。

我剛剛添加了一個templateLoader成員到FileGenerator類,它有自己的getter和setter。

接下來,在我的CreateFile函數,我使用的方法setTemplateLoader(TemplateLoader)從配置類,例如:

public void createFile() { 
    final Configuration cfg = new Configuration(); 
    // Changed 
    cfg.setTemplateLoader(templateLoader); 
    // the rest 
} 

最後,我只是創建了我的測試模板裝載機:

@Test 
public void shouldCreateFile() { 
    final MyFile mockedMyFile = spy(file); 
    final Map<String, Object> rootMap = new HashMap<String, Object>(); 
    TemplateLoader templateLoader = null; 

    try { 
     templateLoader = new FileTemplateLoader(new File("<PATH TO TEMPLATE FOLDER>")); 
    } catch (final IOException e) { 
     e.printStackTrace(); 
    } 

    gerador.setTemplateLoader(templateLoader); 
    // the rest 
} 

並解決問題。在我的生產代碼中,我使用ClassTemplateLoader而不是FileTemplateLoader。

相關問題