Page 1 of 1

JUnit testing hampered by static methods

Posted: Fri Oct 11, 2013 4:05 pm
by jfrantzius
Hi,

while doing some debugging, I came across a good number of static methods for accessing objects stored in thread locals or request context, e.g. CartState.getCart(). That's a bad thing if you want to do JUnit-testing.

Are there any plans to replace static method calls e.g. by injecting singleton-scoped objects? This would definitely make https://github.com/BroadleafCommerce/Br ... t-coverage more realistic ;)

By the way, is there a particular reason why the Cart is request-scoped? From what I understand, it is put into the request by the CartStateFilter, which seems to require a database query on each request (within CartStateRequestProcessor.process(WebRequest)). By putting the cart into the session, the database query would be required only once per session, which I'd find a performance improvement on first sight (requiring more memory in the JVM, though)

Regards,
Jörg

Re: JUnit testing hampered by static methods

Posted: Tue Oct 15, 2013 9:41 am
by phillipuniverse
Putting the full cart in session makes things more difficult in terms of Hibernate. It basically makes it so that you cannot ever trust CartState.getCart() to do anything with Hibernate; if you try to merge what you get out of there it will override what's in the database. Plus, in terms of a Cart, you always want the latest version that you can get your hands on. There's a discussion about this in the context of Customer: https://github.com/BroadleafCommerce/Br ... e/pull/365. Plus, if you're doing Hibernate caching right then it's not that big of a deal.

Good feedback about CartState.getCart() and the like. This seems like it would warrant an issue for some further discussions by others on the team.

Thanks!