Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:09:20+00:00 2026-05-26T00:09:20+00:00

I wrote a functional test to check adding items to a shopping cart.For a

  • 0

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.

the controller method which adds item to cart

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);
}

my test method is

 @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<String,String> addtocartParams = new HashMap<String,String>();
        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;
    }

The console output I get is

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

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.

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?

//test method body ..modified

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:09:21+00:00Added an answer on May 26, 2026 at 12:09 am

    It fails because the cart instance used by your test method and the cart instance used by the addItemToCart method are different. Each transaction has its own instance of the entity. And JPA doesn’t automagically refresh an entity when some other transaction updates the row mapped by this entity.

    You should reload the cart from the database after addItemsToCart has been called to check if something has been added to the cart in database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unit test to check whether a method returns the correct IEnumerable
I wrote a little test case for a model I created in Yii and
Do you write one test per function/method, with multiple checks in the test, or
QA tester was reading HTML/JS code to write a functional test of a web
I wrote this function to get the unread count of google reader items. function
Is it possible to write a doctest unit test that will check that an
Are there standard documents around to write Test Scenarios (or functional tests/ acceptance tests)?
I can't seem to test a function I wrote in PLT Racket using the
How does one write a unit test that fails only if a function doesn't
I am currently trying to write functional tests for a charging form which gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.