2011-12-01 82 views
2

我試圖運行這個功能測試PlayFramework - 運行使用JSON渲染

public class JsonRenderTest extends FunctionalTest {  
     @Before 
     public void setup() { 
      Fixtures.deleteDatabase(); 
      Fixtures.loadModels("data.yml"); 
     } 

     @Test 
     public void testThatJsonRenderingWorks() { 
      Response response = GET("/recipe/1"); 
      assertIsOk(response); 
     } 
    } 

回答這個調用Action功能測試是這樣

public static void showRecipe(Long id){ 
     Recipe recipe = Recipe.findById(id); 
     notFoundIfNull(recipe); 
     renderJSON(recipe); 
    } 

當我使用運行在Firefox測試TestRunner at http://localhost:8080/@tests 我收到此錯誤消息:

失敗,預期響應狀態:< 200>卻被:< 404>

現在,如果我在瀏覽器中運行這個網址http://localhost:8080/recipe/1,我得到的JSON性反應我期待至極是我的食譜對象的JSON表示。

當然還有在數據庫ID爲1

配方現在,這裏是我的問題。爲什麼當瀏覽器沒有時測試失敗。我在Chrome,IE和FF中嘗試了這一結果。

任何指針將不勝感激。 感謝

-Alain

+1

是U使用的是什麼版本的遊戲本,現在運行我的測試針對MEM數據庫的問題? 從燈具加載時,您不應該假設實體的id = 1。 嘗試做一個查找(「byName」)或其他屬性您的食譜對象,然後使用該對象ID構建URL。 – mericano1

+0

謝謝Marco。我正在使用Play-1.2.3。我會嘗試你的建議並回報 – Alain

+0

嘗試了你的建議,沒有區別。 – Alain

回答

0

您可以使用一個輔助方法的內容類型添加到您的要求

public Request jsonRequest(Request request) { 
    request.contentType = "application/json"; 
    request.format = "json"; 
    return request; 

} 

然後你的測試變得

@Test 
    public void testThatJsonRenderingWorks() { 
     Response response = GET(jsonRequest(newRequest)), "/recipe/1"); 
     assertIsOk(response); 
    } 
} 

您也可以爲您WS類做真正的http測試insted。

1

謝謝eveyone。 好吧,我找到了答案。 看來我的測試是在夾具數據完全加載之前運行的。 我正在對本地MySql數據庫運行我的測試。 當我刪除電話

Fixtures.deleteDatabase(); 

測試運行良好。

要解決,我在application.conf文件

%test.db.url=jdbc:h2:mem:play;MODE=MYSQL;LOCK_MODE=0