Thursday Jun 17, 2010

Unit tests and checked exceptions

In the past, when I've seen test methods written by others that use throws Exception in the message signature, I've been irritated. Using throws Exception looks like a lazy attempt to circumvent the requirement to explicitly declare all thrown exceptions. But I think I've changed my mind on this, at least when writing test cases.

The main reason is one of maintainability. If you use some method throughout your test suite, and it is one day updated to throw a new exception, you now have to update the method signature of every single test case that calls that method. And, as far as I can figure, you gain exactly nothing for your efforts.

But, you might object, the requirement to explicitly declare all exceptions is a necessary evil, because it allows any calling code to be written defensively. You can't prepare for exceptional conditions that you don't know about. And that's true, and in most cases, you should continue to explicitly declare all exceptions. But I'd wager that with most test cases, you don't care about exceptions that might occur, except insofar as they cause the test to pass or fail, and you probably don't intend for any other code to call your test case method.

It will be interesting to see what other apparent stupidities eventually reveal their wisdom to me.

Figuring out how to script WebSphere app server with Jython

A single, comprehensive reference document for WebSphere application server's Jython API is doubtless hidden away in the dark, tangled thicket that is IBM's web site. I am as certain of this fact as I am certain that I will never find it. When, long ago, I wrote a script to automate deployments to a WebSphere app server, I cobbled it together using both the odd blog post here and there as well as the vague samples scattered through IBM's documentation -- and some of those samples were written in JACL, not Jython. It almost goes without saying that there has always been an easier way to find this information.

It helps, first of all, to know that four objects are available as global variables to any Jython process that has been created by wsadmin. Those objects are:

  • AdminConfig
  • AdminControl
  • AdminApp
  • AdminTask

You can look at these wsadmin objects if you fire up the interactive Jython interpreter: $WASHOME/bin/ -lang jython, but if you're familiar with Python's built-in introspection commands, like dir() and help(), you'll quickly see that they're of no use to you. Fortunately, the four wsadmin objects implement their own help() methods. To use them, print the output of For example, print prints out the following:

WASX7095I: The AdminApp object allows application objects to be manipulated -- this includes installing, uninstalling, editing, and listing. Most of the commands supported by AdminApp operate in two modes: the default mode is one in which AdminApp communicates with theWebSphere server to accomplish its tasks. A local mode is also possible, in which no server communication takes place. The local mode of operation is invoked by bringing up the scripting client with no server connected using the command line "-conntype NONE" option or setting the "" property in the

deleteUserAndGroupEntries    Deletes all the user/group information for all the roles and all the user name/password information for RunAs roles for a given application.
edit    Edit the properties of an application
editInteractive    Edit the properties of an application interactively...

Calling help() with the name of a command provides details on that command. So print"edit") prints out:

WASX7104I: Method: edit

Arguments: application name, options

Description: Modifies the application specified by "application name" using the options specified by "options". The user is not prompted for any information.

This is absolutely basic stuff, but it took me forever to find it. Later, I'll cover the basics of scripting a deployment, which will exhaust my knowledge of this subject.


A weblog about identity management and testing. See here.


« June 2010