在为基于 play1.2.4 的 webapp 编写功能测试时,我对如何正确编码有点困惑。困惑在于所涉及的事务边界。我在某处读到每个测试都有自己的事务。
在我的应用程序中,用户可以登录并将一些物品添加到购物车中。然后他可以提供一个地址,以便可以将物品发送给他。
我创建了如下的私有辅助方法
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;
}
最后,我编写了函数测试,它调用这些辅助方法中的每一个来执行任务,然后执行断言
@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
在
POST("/items/address/"+joncart.customer.getId().toString(),addressParams);
我认为这是由于某些事务边界问题而发生的,但我不确定..有人可以帮我纠正这个吗?..我真的很想在这种情况下如何编写测试方面得到一些帮助..