2011-12-01 83 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