Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
OSGi is just a load of fancy class loaders! Oh, wait, we didn’t mean that sort of mocking. (Besides, we all know by now that there’s a lot more to OSGi than enhanced class loading.) We’re talking about using mock objects to test portions of code without requiring a complete system. A mock object is basically a simulation, not a real implementation. It provides the same API, but its methods are scripted and usually return expected values or additional mocked objects. Object mocking is a powerful technique because it lets you test code right from the start of a project, even before your application is complete. You can also use it to test situations that are hard to recreate with the real object, such as external hardware failures.
Figure 7.5 represents the dynamic log client from chapter 4. Let’s take this example and test it outside OSGi by mocking the API: verifying calls made to the API and scripting the desired responses. We’ll show you how easy it is to script scenarios that may be hard to reproduce in a real container, look at mocking in a multithreaded environment, and wrap things up by reliably demonstrating the race condition mentioned in section 4.3.1.