Error calling method on NPObject

I’ve had a bit of a problem with a Java Applet running in Safari on Windows. I have JavaScript code which attempts to invoke public methods in the Applet, but whenever I try to call these I get the error “Error calling method on NPObject”.

I’ve read a fair amount of BS “causes” (from things like “it’s a hidden DIV” to “Upgrade your JRE”), none of which proved to the the problem.

The real problem?

Safari doesn’t care if the applet is loaded before trying to invoke the methods.

Under Chrome, Opera, Firefox and even my much hated Internet Explorer, if you try to invoke a method on an applet which hasn’t loaded yet, the JavaScript runtime waits for the applet to complete, and then resumes. Safari does not.

So now, as part of the applet tag’s parameters, I have to include a method name which the applet must invoke in JavaScript, thereby guaranteeing that the applet is loaded and running.

Now I have the problem that apparently Safari is trying to execute JavaScript methods as URLs, thus causing a “Malformed URL” error.

Update:

After a bit of experimenting, it seems Safari does not like trying to pass and return non-String values. So, instead of using LiveConnect’s “call” method, you have to use “eval”, and don’t expect to be able to pass object around.

Partial Mocking/Stubbing and why it’s not evil.

Over the last few years, I’ve started expanding the places where I use automated testing (both test driven development and behaviour driven development, even though one would have to admit that in reality there’s little difference between them). I had only every written very simplistic JUnit tests for Java modules, but never really for the greater overall project. This changed when I starting working on a Ruby on Rails, where we were required to make extensive use of RSpec.

One of the things I learned to appreciate was the heavy use of stubbing (and expectations) that RSpec provided. Obviously, this was easier to do for a dynamic language such as Ruby (ignoring all the other shortcomings of the language itself).

When I started writing Java applications again, I obviously wanted to be able to perform similar tests with similar stubbings. To my dismay, the more pedantic amongst us programmers somehow decided that partial mocking/stubbing was somehow a truly terrible thing to do. I began reading all sort of justifications for why it was bad, and how one should refactor the class being tested in order to test it.

jMock and Mockito both adhered to this belief, EasyMock seemed less stringent, but stubbing was overly string-based (making refactorying later on a little annoying since tests would fail for no other reason than renaming a method).

Following is an example of the EasyMock mechanism (as shown in their documentation)

ToMock mock = createMock(ToMock.class,
   ToMock.class.getMethod("mockedMethod", null));

Unfortunately, all these folks fail to take into account the real world, and situation under which you code might not want to allow users to screw around with dependent objects. Here is a real example:

I created a class which did a load of work (let’s call it FlowClass), and part of this work was to use another class (let’s call it LoaderClass) which started up operating system processes. I needed to test the flow of FlowClass, but I didn’t want to start firing off all sorts of new processes. Easily enough, I was able to move the instantiation of LoaderClass into another method, but using something like jMock, how do I prevent that one single method from causing processes to load? I emphatically do not want the user to have to set instance of LoaderClass (like a JavaBean property), because I needed to keep strict control over the flow. The idea of having to introduce potential erratic program behaviour simply in order to test is completely unacceptable by any stretch of the imagination.

One site I came across suggested subclassing FlowClass directly in the test (even though, once more, the author makes all sort of claims of this being evil but delicious). This did the job, but it seems like so many lines of code to do nothing more than force a value to be returned. On top of that, what if I wanted to check to make sure the method was even ever called?

My needs eventually led me to create JavaStubs. It doesn’t do all the work that the other mocking frameworks do, but what it does do is allow me to quickly and easily stub method, including capturing exceptions for testing, and counting how many times they were called. I tried to be as string-less as possible (thank goodness for Java generics), and make it easily readable (much thanks to Mockito for suggestions on that one).