【问题标题】:Play Framework 1.x Functional Test with @AllowFeature Controller Method使用@AllowFeature 控制器方法播放Framework 1.x 功能测试
【发布时间】:2021-09-07 07:15:24
【问题描述】:

我确实想为我们的项目编写一些功能测试。 Techstack:Play Framework 1.5、Java 16、Junit 3。

我找到了以下文档: test - 1.5.x security - 1.5.x

所以控制器看起来像这样。

@AllowFeature(Feature.SEARCH_BOX)
public static void search(String term) {
    //implementation omitted

    TablePage<MyAwesomeType> page = search(term);

    render(..., page, term);
}

我的测试看起来像这样

public class SearchTest extends FunctionalTest {

    @Test
    public void search_withResults() {
        String term = "ABC";
        Http.Response response = GET("/foo/search?term=" + term);

        assertStatus(302, response);

        assertThat(renderArgs("page"), is(notNullValue()));
        TablePage<MyAwesomeType> page = (TablePage<MyAwesomeType>) renderArgs("page");

        assertTrue(page.getTotalRecords() >= 1);
    }

}

但是,TablePage&lt;MyAwesomeType&gt; page 确实不应该是 null,我无法使用调试器进入控制器方法。所以看起来控制器方法search(...)根本没有被调用。

响应代码是302 - Found,但我认为这可能是游戏建议它找到了路径/foo/search

我的猜测是我需要设置一些UserContext 或发送authenticityToken 连同请求。所以play可以检查需要的功能@AllowFeature(Feature.A_SEARCH_BOX)

有人知道我将如何设置这样的功能测试吗?

感谢任何帮助。谢谢!

【问题讨论】:

    标签: java playframework-1.x junit3


    【解决方案1】:

    我能够弄清楚这一点。

    我需要登录应用程序,然后播放 FunctionalTest.class 处理 cookie。

    1. 在登录方法中添加@NoAuthenticity
    @NoAuthenticity // <-- This enables execution without authenticityToken
    public static void login(@Required String username, @Required String password) {
        ... 
    }
    
    1. 在测试前发布登录请求。
    @Test
    public void search_withResults() {
        // 1. login
        Map<String, String> credentials = Map.of("username", "MyUsername", "password", "MyPassword");
        POST("/login", credentials); 
        // Note: session info / authenticityToken is stored in a cookie
        // FunctionalTest.class makes sure to use this cookie for subsequent requests
    
        // This request now works like a charm
        String term = "ABC";
        Http.Response response = GET("/foo/search?term=" + term);
    
        assertStatus(302, response);
    
        assertThat(renderArgs("page"), is(notNullValue()));
        TablePage<MyAwesomeType> page = (TablePage<MyAwesomeType>) renderArgs("page");
    
        assertTrue(page.getTotalRecords() >= 1);
    }
    

    注意:可以使用 JUnit @Before 注解来简化测试类。

    @Before
    public void login(){
        Map<String, String> credentials = Map.of("username", "MyUsername", "password", "MyPassword");
        POST("/login", credentials);
    }
    
    @Test
    public void search_withResults() {
        String term = "ABC";
        Http.Response response = GET("/foo/search?term=" + term);
    
        assertStatus(302, response);
    
        assertThat(renderArgs("page"), is(notNullValue()));
        TablePage<MyAwesomeType> page = (TablePage<MyAwesomeType>) renderArgs("page");
    
        assertTrue(page.getTotalRecords() >= 1);
    }
    
    @Test
    public void anotherTest() { ... }
    
    @Test
    public void yetAnotherTest() { ... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多