JUnit and Netbeans. Injecting in objects in the default Lookup

If you need to unit test classes in a netbeans module and the classes use the Lookup.getDefault() to “lookup” things that you want to mock you probably wonder how you can place the mock objects in the default Lookup Adding objects to the Netbeans’s default Lookup is relatively simple. Here is the answer: Replace the `Lookup` instance returned by the `Lookup.getDefault()` by defining a new entry in `META-INF/services`. It’s just a matter of adding a file named org.openide.util.Lookup$Provider in the META-INF/services folder inside your Unit Test source folder. That file should contain the fully qualified class name of your own Lookup.Provider implementation. In my case the file contain just the string com.rubenlaguna.en4j.searchlucene.NoteFinderLuceneImplTest because the test class itself implements Lookup.Provider. ...

February 12, 2010

CGLib, NetBeans Modules and class loaders

My first try at using CGLib from a Netbeans RCP ended up in a ClassNotFoundException for net.sf.cglib.proxy.Factory I had two modules, MainModule and cglib and MainModule was depending on cglib. And the following snippet was raising the ClassNotFoundException. Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Graph.class); enhancer.setCallback(NoOp.INSTANCE); Graph g = (Graph) enhancer.create(new Class[]{FrameListener.class,InterpreterAbstraction.class},new Object[]{fl,ia}) ; As I later found out from Enhancer documentation and DevFaqClassLoaders it seems that Enhancer it taking the System class loader instead of taking the Module classloader. I explicitly set the classloader used by the Enhancer via the setClassLoader method and now it seems to work. ...

August 6, 2009

Opening Favorites window at startup in a Netbeans Platform Application

After learning about the Favorites window in the NetBeans Platform Quick Start. I wanted to use it in an application of my own. This easy task became more difficult that I though because I was getting random results, sometimes after a clean build I got the Favorites window open and sometimes not. But let’s start from the beginning, I put my hands at work, it was just a matter of adding the Favorites module to the Netbeans Platform Application (app’s properties ⇒ Libraries ⇒ Platform Modules ⇒ platform9 ⇒ Favorites) and edit one of my modules layer.xml to override Windows2/Modes/explorer/favorites.wstcref. Just go to Important Files ⇒ XML Layer ⇒ this layer in context ⇒ Windows2 ⇒ Modes ⇒ explore ⇒ favorites.wstcref and replace ...

April 28, 2009

Attach source code to a Netbeans Library Wrapper Module

I’m new in NetBeans and today I’ve been struggling a couple of hours with the simple task of attaching the source code for an external JAR (NetBean Library Wrapper Module). I’ve been trying to find in google how to do it without success until I reached this post in the netbeans-users mailing list. Then I realized that I should been looking into the NetBeans Help first. The entry at Help -> Help contents -> Java Applications -> Debugging Applications -> Attaching Source Code to a JAR File explains how to do it. ...

February 22, 2008