Kevin
Kevin

Reputation: 6833

JUnit testing and Google Guice

I am a beginner to JUnit and design patterns, so please pardon me.

I was reading this technical webpage:

http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&sqi=2&ved=0CB4QFjAA&url=http%3A%2F%2Ftechruminations.wordpress.com%2F2011%2F05%2F30%2Fsales-tax-problem-form-thoughtworks-for-interview%2F&ei=QBVjUIHgDYWAiQefzoHoBQ&usg=AFQjCNFIBDF10v9zEMRpAIgl9Yi7DZ172w&sig2=E-Dwtx9UaU4lBbyD0j9bww

I can understand the actual coding and class organization etc. The only thing that confuses me are the two questions below related to this particular problem:

  1. It used a design pattern called "dependency injection" by using Google's Guice, however, I found that I could get around without using this design pattern achieving the same function. I wonder using dependency injection is not a must in this problem?

  2. Since I am new to JUnit, my understanding is that he wrote test cases for cart,item and test them individually to prove the correctness. But why does he need the final "Build and Deployment" section to compile and run the test program, since I can easily do that without creating that xml file and just do right-click in Eclipse and select "run as JUnit test".

Please give me some help on JUnit and design patterns, thank you!!

Upvotes: 1

Views: 883

Answers (1)

Jeff Bowman
Jeff Bowman

Reputation: 95614

  1. Guice is a dependency injection framework, which is a very useful tool for managing long chains of objects and their dependencies, and for changing implementations later. It is not strictly necessary for this problem, or for any problem, but you may find it very useful in certain circumstances.

    • In the example you posted, there is only one implementor of ItemIntf, and only one implementor of CartIntf, and only one implementor of TaxCalculatorIntf. Because these are bound with Guice, you could later write another implementation of CartIntf (say, SessionBasedCart) and change which implementation your classes use just by changing that single line in the module.
    • Likewise, if Item needed an additional dependency, such as ItemCatalog, you would not need to change any of the other files. Item could simply inject ItemCatalog.
    • Finally, with a dependency injection framework, you may find that your classes are easier to test. For a class like ServiceTaxCalculator, if it calls to any external dependencies, it is very easy to write a test that passes in those dependencies (rather than letting your class use the new operator and then replacing them later).
  2. The Build and Deployment section refers to a tool called Ant, which is a way of using XML to specify build instructions and dependencies such as JUnit. Eclipse has a set of built-in tools for managing the classpath and managing dependencies on JUnit, which means that you do not need to build one of those on your own. However, if you wanted to run your tests outside of Eclipse, you may need to create a file similar to that one.

You can read more about Apache Ant to see exactly what it can do for your project, and likewise read more about Google Guice and why dependency injection can be a good idea for some projects.

Upvotes: 2

Related Questions