Revision as of 18:28, 6 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

Lookup is a general API for registering and querying instances of services or other objects. There are two basic uses of lookup: a particular context, e.g. a node selection passed to an action; and global default lookup, used to register services in the system.

When writing a unit test that tests code which (directly or indirectly) calls Lookup.getDefault() you should consider what the available instances will be. By default, any classes registered in META-INF/services/* resources in JARs in the test classpath will be available.

If you want to override this, the easiest thing is to use MockServices, from the nbjunit test utilities library. For example, you may want to register a special ProjectFactory for use within one test, because you want a simplified project type you can control to test some features of code which works with projects. Here is how:

protected void setUp() throws Exception {
public static class TestingProjectFactory implements ProjectFactory {
    // implement interface methods...

Now e.g. ProjectManager.getDefault().findProject(...) should give TestingProjectFactory a chance to recognize the project.

Note that TestingProjectFactory will be available in default lookup as the first ProjectFactory instance, but any others registered in META-INF/services/* will still be available "later". (Lookup is ordered.) Many services placed in lookup (e.g. DialogDisplayer) are intended to be singletons, in which case installing an instance using MockServices effectively means you can override the default implementation.

You can also add and remove services while the test runs. Every call to setServices overrides the previous call.

Code which directly looks in META-INF/services/* - for example, calls to java.util.Service, as well as many subsystems such as JAXP - should also see services registered this way.

If you wish to register individual instances to default lookup, without the requirement that they be default instances of public classes, you can also use MockLookup. See UsefulTestClassesInModules.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo