在Playframework中编写FunctionalTest的正确方法 [英] proper way of writing FunctionalTest in playframework

查看:124
本文介绍了在Playframework中编写FunctionalTest的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于play1.2.4编写web应用程序的FunctionalTest时,我对如何正确编写代码有些困惑.这种困惑是关于所涉及的事务边界的.我读到某个地方,每个测试都有自己的事务.

While writing FunctionalTest for a webapp based on play1.2.4,I was a little confused as to how to code it properly.The confusion is regarding the transaction boundary involved.I read somewhere that each test has its own transaction.

在我的应用中,用户可以登录并向购物车中添加一些物品.然后,他可以给出一个地址,以便将物品发送给他.

In my app,a User can login and add some items to the cart.Then he can give an Address sothat the items can be sent to him.

我创建了如下的私有帮助器方法

I created private helper methods like below

private Cart buyItemsAsCustomer(String email,String pass) {
    User user = User.find("byEmail", email).first();
    //login
    Response loginResponse = loginAsUser(email,pass);
    Cart cart = new Cart(user);
    cart.save();
    addItemsToCart(cart.id);
    return cart;
}
private Response loginAsUser(String email,String password) {
    Map<String,String> loginUserParams = new HashMap<String,String>();
    loginUserParams.put("username", email);
    loginUserParams.put("password", password);
    Response response = POST("/login",loginUserParams); 
    return response;
    }

private Response addItemsToCart(Long cartId) {
    Cart cart = Cart.findById(cartId);
    Item item = Item.find("byIsbn","978-0451160522").first();
    Map<String,String> addtocartParams = new HashMap<String,String>();
    addtocartParams.put("cartId", cart.id.toString());
    addtocartParams.put("quantity", "2");
    String addtocarturl = "/items/addtocart/"+item.id.toString();
    Response response = POST(addtocarturl,addtocartParams);
    cart.refresh();//without this the assertion about number of items in cart fails!
    return response;
}

private Map<String,String> createAddressParams(){
    Map<String,String> addressParams = new HashMap<String,String>();
    addressParams.put("addressline1", "1123,xx street");
    addressParams.put("state", "new york");
    addressParams.put("country", "US");
    return addressParams;
}

最后,我编写了FunctionalTest,它调用了这些帮助方法中的每个方法来执行任务,然后执行断言

Finally I wrote the FunctionalTest which calls each of these helper methods to do the tasks and then does the assertions

@Test
public void testCustomerCanAddAddress() {
    Fixtures.loadModels("data.yml");
    assertTrue(Address.findAll().size()==0);
    Cart joncart = buyItemsAsCustomer("jon@gmail.com","jon");
    assertFalse(jonart.cartItems.size()==0);
    Map<String,String> addressParams = createAddressParams();
    POST("/items/address/"+joncart.customer.getId().toString(),addressParams);
    assertTrue(Address.findAll().size()==1);
}

运行此代码会产生

A java.lang.RuntimeException has been caught, java.util.concurrent.ExecutionException: play.exceptions.JavaExecutionException at

POST("/items/address/"+joncart.customer.getId().toString(),addressParams);

我认为这是由于某些事务边界问题而发生的,但是我不确定.有人可以帮我解决这个问题吗?.我真的很希望在这种情况下应该编写测试方面的帮助.

I think this happens because of some transaction boundary issue ,but I am not sure..Can someone please help me correct this?..I really would like some help as to how the test should be written in this case..

我已将堆栈跟踪信息粘贴到此处

I have pasted the stacktrace here

推荐答案

您的测试很奇怪,因为您混合了http调用和模型调用.功能测试纯粹是http调用.您测试控制器层

Your test are weird because you mix http calls and models calls. A functional test is purely http calls. You test your controllers layer

如果您需要一些模拟数据,则必须在另一个事务中创建它们,对我而言,最简单的方法是使用特定方法的作业.如果您想检索数据以确保测试通过,也是同样的事情.

If you need some mock data you have to create them in another transaction, the easyest way to do that for me is to use a job in specific method. Same thing if you want to retrieve data to ensure your test passed.

@Before
public void cleanUp() throws Exception {
    new Job() {
        @Override
        public void doJob() throws Exception {
            Fixtures.deleteDatabase();
            Fixtures.loadModels("data.yml");
        }

    }.now().get();
}

这篇关于在Playframework中编写FunctionalTest的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆