9/25/2011

Dependency Injection (DI) in Java EE

In a J2EE/JavaEE server, container has always been the backbone for creating and managing the lifecycle of resident components. It's not a surprise to see some forms of DI is already present at the beginning of J2EE. For example, javax.ejb.SessionBean, the legacy interface implemented by old-style EJB bean class, contains a setSessionContext(SessionContext ctx) method, which is called by ejb container to inject a SessionContext to the bean class. Similarly, javax.ejb.EntityBean has setEntityContext(EntityContext ctx), and javax.ejb.MessageDrivenBean has setMessageDrivenContext(MessageDrivenContext ctx) callbacks.

Another less obvious example is Servlet init methods, which are invoked by servlet container to initialize ServletContext and ServletConfig for servlet class:

//Declared in javax.servlet.Servlet interface and implemented in GenericServlet:
public void init(ServletConfig config)

//Convenience method implemented in GenericServlet
public void init();
This early forms of DI is typically defined as part of the lifecycle callbacks and strongly typed to a specific interface or abstract class.

DI capability was substantially expanded in Java EE 5 with the introduction of annotation- and xml-based DI. Both field- and setter-injections are supported, and xml deployment descriptors can be used to override or augment annotation meta data. Most common ones are:
@EJB private Calc calc;

@EJB(name="ejb/calc", beanName="DefaultCalc", beanInterface=com.my.ejb.Calc.class)

@Resource SessionContext ctx;

@Resource private String widgetName;

@Resource(mappedName="custom/widgetName")
private String widgetName;

@PersistenceContext private EntityManager em;

@PersistenceContext(unitName="advanced-pu", type=TRANSACTION)
private EntityManager em;
As you can see, the annotation type is specific to each resource type. The advantage of this approach is, you can easily customize how a specific type of dependency is constructed and initialized. This design makes sense as some resources need more initialization params than others. But the downside is lack of internal consistency across the board. For each new type of resources, we need to decide whether to reuse the existing annotations, add new attributes to existing annotations, or create a new one. This task is tackled in Java EE 6, with JSR 330 (AtInject spec) and JSR 299 (CDI spec), both are required component specs in Java EE 6 Platform.

Now in Java EE 6, all the above is doable with a single @java.inject.Inject. For injections with no attributes (or default attributes), we can easily replace @EJB/@Resource/@PersistenceContext with @Inject. Otherwise, a @javax.inject.Qualifier is needed to provider more information. Basically we are promoting annotation attributes to auxiliary annotations, replacing string-based qualifiers with typed qualifiers, and chaining them together to resolve the DI.

@EJB and @Resource are also enhanced in Java EE 6 to take an additional portable attribute lookup, mainly as a replacement for the nonportable mappedName attribute:
@EJB(lookup="java:global/myApp/myEJBModule/CalcBean")
private Calc calc;

@Resource(lookup="java:module/env/sleepSeconds")
private long sleepSeconds;

9/23/2011

JUnit Notes, Do's and Don'ts

A skeleton JUnit 4 test case:

import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;

public class MyTest {
/** 4 optional lifecycle callback methods */
@Before public void before() {} //must be public void no-arg

@After public void after() {} //must be public void no-arg

@BeforeClass public static void beforeClass() {} //must be public static void no-arg

@AfterClass public static void afterClass() {} //must be public static void no-arg

@Test public void testAdd() {
int a = 1, b =2, expected = 3;
int actual = a + b;
assertEquals(expected, actual);
}
}
Do not name your test main class Test.java, which has the same short name as @Test annotation. Unless you explicitly use @org.junit.Test, @Test will resolve to your test class, instead of org.junit.Test annotation type.

Avoid using Java assert; use static assertXXX methods (statically imported) in org.junit.Assert class. Java assert is turned off by default, and can be enabled only at command line with "java -ea", or "java -enableassertions" options. So some tests using Java assert will always pass when running with the default configuration, regardless of the actual test conditions. For example, the following method passes unless -ea option is passed to java:
@Test public void testNumber() {
assert(1 < 0);
}

java -cp $tmp/junit-4.8.1.jar:. org.junit.runner.JUnitCore MyTest
JUnit version 4.8.1
Time: 0.009
OK (4 tests)
A secondary reason to favor org.junit.Assert.assertXXX over Java assert is, the former is a richer API with more descriptive output, whereas Java assert, in its common form, only fails with java.lang.AssertionError and stack trace with no description. Java assert can also provide details with a second param: assert false : reason

maven surefire plugin automatically enables Java assertions when running JUnit tests on JDK 1.4 and above. So when running with surefire, there is no problem of false positive test results. But still I would like my JUnit tests to work the same way across all testing environment. To disable Java assertion in mvn surefire:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<enableAssertions>false</enableAssertions>
</configuration>
</plugin>
</plugins>
</build>
If your tests passed in Eclipse or ant, but failed with mvn, chances are some java assertions failed the tests. Check target/surefire-reports/* for any AssertionError.

Avoid using assertTrue(list1.equals(list2)); use assertEquals(list1, list2) instead. assertTrue will only print out the useless message like "expecting true, but actual false", whereas assertEquals gives more helpful message like "expected:[null, 1, 2], but was:[1, 2]"

Do not add assertTrue(true) at the end of a @Test to signal the success result. It's just not needed. The test simply passes when nothing goes wrong.

@Test method can declare throws clause, e.g., throws NamingException. When an exception is thrown during test run, the test is failed. So use exception as a means to communicate test failures. Do not try-catch an exception, just to print it and manually fail() it. For example, do this:
@Test public void testNumber() {
int i = 10 / 0;
}
Do NOT do this:
@Test public void testNumber() {
try {
int i = 10 / 0;
} catch (ArithmeticException e) {
e.printStackTrace();
fail(); // or even worse, assert(false);
}
}
When running the try-catch version, ArithmeticException stack trace is printed, followed by another stack trace of java.lang.AssertionError from calling fail(), whereas in the shorter version, only the ArithmeticException is logged and hence much easier to trace.

How to run a single test? org.junit.runner.JUnitCore, the default command-line runner, takes a list of test classes as input, but doesn't support running a single test method. Other tools and IDE may support it by providing their own runner. With maven surefire 2.7.3+ and JUnit 4 tests, it is supported with a -D sysprop (link):
mvn test -Dtest=com.my.test.TestMain#test1
mvn test -Dtest=com.my.test.TestMain#*egative*
mvn org.apache.maven.plugins:maven-surefire-plugin:2.8:test -Dtest=com.my.test.TestMain#test1
I prefer to put the test name at the very end of the line so it's easier to go through the history and edit command.

To check the version of your JUnit:
$ java -cp $tmp/junit.jar junit.runner.Version
4.8.1
To skip or exclude a test, use @Ignore (org.junit.Ignore).