Java Reference
In-Depth Information
M YCILA T ESTING F RAMEWORK
Mycila ( http://code.google.com/p/mycila) is an umbrella for many subprojects, each
one focused on particular needs, such as testing. It's the latest offspring of this new
breed of general-purpose testing libraries (which also includes Unitils and FEST ), and
at the time this chapter was written it was still in its infancy. Although all of its features
we analyze in this chapter are provided by other tools, this project also offers unique
features, such as a module to test Guice 3 -based applications; if it fulfills its ambitious
goal of providing “Powerful projects for everyday needs!” it could be another valuable
asset in the toolbox.
19.1.2
Running the examples
The test cases for this sample application are available in two flavors: Maven and Ant.
To run the tests on Maven, run mvn clean test . Similarly, to run them using Ant, type
ant clean test . Some of these tools might require esoteric dependencies at runtime
(for instance, Unitils database support uses Spring for transaction management), but
all such dependencies are commented in the build.xml file. The application is also
available as two Eclipse projects, one with the required libraries and another with the
project itself.
Now that all introductions have been made, let's get down to business.
19.2
Transparent mock usage
When you use mocks in your test cases, 4 the test method is typically structured
as follows:
Create an instance of the object being tested.
1
Create mock instances for dependent objects, and inject them into the
tested object.
2
Set mock expectations.
3
Call the method being tested.
4
Optionally, verify mock expectations.
5
You could manually write these five steps in every test (that's the approach we've taken
so far, in chapters 6 and 16), but as you learned in chapter 17 (section 17.7.1), such
repetitive workflow is a strong candidate for refactoring through the Template Design
Pattern. Only steps 3 and 4 are test specific; all other steps are pretty much the same
for all tests.
In this section, we analyze three tools that provide infrastructure for transparent
mock usage, and we refactor the existing UserFacadeImplTest (originally defined in
listing 18.5) to use each of them.
3
Guice ( http://code.google.com/p/google-guice) is “a lightweight dependency injection framework for Java
5 and above, brought to you by Google.” In other words, it's the simplified Google-based version of Spring.
4
Mocks are explained in more detail in chapter 7.
 
 
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search