Note that there are some explanatory texts on larger screens.

plurals
  1. POfunctional test in playframework fails when adding items to cart
    text
    copied!<p>I wrote a functional test to check adding items to a shopping cart.For a user to be able to add items to cart,he needs to login.So,I created a method to login the user and another method to add the item.Before and after the addtocart method in test,I am checking the size of content of cart.The addtocart functionality works without any problem when I run the app in dev mode(I can check the db too-which is postgres and not an in memory db).The addtocart fails in test.</p> <p>the controller method which adds item to cart</p> <pre><code>public static void addItemToCart(Long productId,Long cartId,String quantity) { Product product = Product.findById(productId); ShopCart cart = ShopCart.findById(cartId); int qty = Integer.parseInt(quantity); CartItem cartItem = new CartItem(product,qty); cart.addItem(cartItem); cart.save(); System.out.println("Controller::addItemToCart()::cart id="+cart.id+" has="+cart.cartItems.size()+" items); } </code></pre> <p>my test method is</p> <pre><code> @Test public void testUserCanAddItemsToCart() { Fixtures.loadModels("data.yml"); User user = User.find("byEmail","user@shop.com").first(); loginAsCustomer("user@shop.com","userpass"); ShopCart usercart = new ShopCart(user); usercart.save(); System.out.println("BEFORE ADD::usercart="+usercart.id+" has :"+usercart.cartItems.size()+" items"); assertTrue(usercart.cartItems.size()==0); addItemsToCart(usercart); System.out.println("AFTER ADD::usercart="+usercart.id+" has :"+usercart.cartItems.size()+" items"); assertFalse(usercart.cartItems.size()==0);//why does this fail? } private Response addItemsToCart(ShopCart cart) { Product pdt = Product.find("byIsbn","654-0451160522").first(); assertNotNull(pdt); System.out.println("addItemsToCart():BEFORE ADD cart="+cart.id+" has="+cart.cartItems.size()); Map&lt;String,String&gt; addtocartParams = new HashMap&lt;String,String&gt;(); addtocartParams.put("cartId", cart.id.toString()); addtocartParams.put("quantity", "2"); String addtocarturl = "/items/addtocart/"+pdt.id.toString(); Response response = POST(addtocarturl,addtocartParams); System.out.println("addItemsToCart():AFTER ADD cart="+cart.id+" has="+cart.cartItems.size()); return response; } </code></pre> <p>The console output I get is</p> <pre><code>BEFORE ADD::usercart=48 has :0 items addItemsToCart():BEFORE ADD cart=48 has=0 Controller::addItemToCart()::cart id=48 has=1 items addItemsToCart():AFTER ADD cart=48 has=0 AFTER ADD::usercart=48 has :0 items </code></pre> <p>Here, in the controller method, the cart instance (of id=48) has 1 item after it is saved to db.But in the test method ,the cart instance of same id has 0 content.</p> <p>I commented out the assertFalse method and retrieved the cart from db using the cartId.Even then the cart of same id has 0 content.I can't understand why this is happening..can anyone shed some light?</p> <p>//test method body ..modified</p> <pre><code>ShopCart cart = ShopCart.findById(usercart.id); System.out.println("AFTER ADD::cart="+cart.id+" has :"+cart.cartItems.size()+" items"); assertFalse(cart.cartItems.size()==0);//why does this fail? </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload