The Art of Writing Software

Unit Testing Setters and Getters for Java Base Types

Series [ Unit Testing Setters and Getters ] Tags [ base types, Java, unit test ]

Yesterday we took a look at some “stock” unit tests for Java bean-style setters and getters, where the underlying property was an object. The tests for properties with base types will be similar, but slightly different. One nice thing about the object tests are that they can use the assertSame assertion (essentially that two objects are == each other) to make sure that the setters/getters do exactly what you thought they would.

Unit Testing Setters and Getters

Series [ Unit Testing Setters and Getters ] Tags [ EasyMock, getters, setters, Java, JUnit, mock objects, unit test ]

So here’s a question for the masses: is it worth it to unit test simple setters and getters? I would contend that if you can automatically generate the code for them, you can also automatically generate the unit test code that tests them (I smell a new ELisp command coming…). So what code should this tool actually generate? Here’s my first cut, assuming we’re going to use EasyMock: Let’s say the name of the bean is foo, and it has class Foo (we’ll go into unit testing beans of base type in a later post).

More on Calling Domain Object Methods From JSPs

Series [ Calling domain objects from JSPs ] Tags [ AJAX, controller, domain object method, DTO, Java, MVC, service, SOAP ]

This is to follow up from yesterday’s post about calling domain object methods from JSPs. If you’ll recall, there were three methods proposed: controller call: make the call in the controller, and pass the results as extra entries in the model data transfer object: put the logic in a special object that provides no-argument methods for retrieving the data in the JSP page taglib: create a taglib that essentially just allows the JSP to call the method itself Having thought a bit about this for a day on the back burner, I’m pretty sure that going the taglib route is not quite the appropriate one here, at least for this method call (which was essentially a range query); so I think it comes down to either the controller call or the DTO.

How to call domain object methods from JSP

Series [ Calling domain objects from JSPs ] Tags [ controller, domain object method, DTO, Java, JSP, JSP method calls, MVC, software design, taglib ]

It seems like the earlier post about super() spawned at least a little (offline) discussion, so here’s another comparison of some different ways of approaching a problem. People should definitely feel free to post some comments , lend your opinions, offer up code snippets, etc. Ok, so here’s the deal. We have a middleware domain object that has a method with arguments, and we essentially want to call that method from a JSP page.

Chatty unit tests

Tags [ commons-logging, JUnit, logging, unit test ]

Did I mention this blog is going to be a bit stream-of-consciousness, hopping around from topic to topic? Anyway, wanted to talk today about “chatty” unit tests–unit tests which produce logging output when they run. Most of the time this is due to logging statements from the class under test. Now, most of our Java classes use commons-logging in the following fashion: public class Thing { private static final Log logger = LogFactory.

Calling super() considered harmful?

Tags [ inheritance, Java, subclassing, super ]

Had an interesting discussion yesterday about how to solve the following problem: * You have a class (in our case, it was a Spring MVC controller) which has some dependencies injected, and which you, being a good defensive programmer, want to check that all the needed dependencies are present. * Since this is a Spring app, it’s ok to use the InitializingBean’s afterPropertiesSet method somewhere in here. * We want to derive some subclasses from the base class that introduce additional dependencies that we want checked, but we want to continue checking the superclass’s dependencies.

Programmable IDEs

Tags [ Emacs, IDE ]

Ok, I’ll admit it. I still use Emacs as my “IDE”. It’s what I started programming with when I was learning Unix/C in college, and it’s always served my needs well. Nowadays, I pretend to be a Java developer, and Emacs still works fine for me. I’ve been meaning to try Eclipse for some time now, but have ended up having issues getting it to work on my 64-bit RHEL workstation, and I just haven’t had time to make it work yet.

Code Craftsmanship

Writing software is as much a craft as furniture making. Someone can ask for a chair, and different furniture makers will produce different chairs. Hopefully most of them will support your weight when you sit in them, but the sturdiness and aesthetics will vary, as will, most likely, the particular joints, types of wood, etc. Obviously, writing code is quite similar; there’s getting the job done, and then there’s getting it done well.