2007-08-03

Importing Public Static Methods

Consider the way you get "assertNotNull" into a JUnit4 test. You do it like this:


import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;



The public static methods of the class Assert are pulled into your lexigraphical context and now you can write code like this in your methods:


@Test
public void testStringyGoodness() throws Exception {
String next = null;
assertNull(next);
next = "foo";
assertNotNull(next);
assertTrue(next.equals(foo));
}


And, all of this is relatively simple... but is it good? I guess that the Object Oriented Paradigm is so well established now that we can do things like this in the language and not expect "old skool" programmers to immediately break things with it.