Testing this is certainly done using the Arquillian framework. If you do not know that yet and you are a Java EE developer you should definitely try that out.

A first positive test should look like:

Create a file in the directory referred to by the activation spec Check that the MDB was called with the right path

The fact that the MDB is called asynchronously adds an additional difficulty to the test. The first naïve approach would be to wait a certain amount of time and check if a method has been called on the MDB. But that will make your tests run very long even when everything is ok and the MDB is called immediately. Or your tests could become fragile if you wait for a too short amount of time.

Andrew Lee Rubinger and Aslak Knutsen propose an improved approach in their book "Continuous Enterprise Development in Java" using the class java.util.concurrent.CyclicBarrier added by Java SE 7. It implements a barrier that most of us probably know from the CS lectures in parallel programming. Together with CDI events this makes test pass immediately if everything works fine and make it only wait if there is a failure.

So the idea is that the MDB fires a CDI event if a method is called. This event is observed by test class that walks into the barrier. The test method is the second party going into the barrier.

The test MDB basically looks like this:

@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "dir", propertyValue = ".") }) public class FSWatcherMDB implements FSWatcher { @Inject private Event<FileEvent> fileEvent; @Create(".*\\.txt") public void onNewTextFile(File f) { fileEvent.fire(new FileEvent(FileEvent.CREATE, f)); } ... }

The test class looks like this:

@RunWith(Arquillian.class) public class ResourceAdapterTest { @Deployment public static EnterpriseArchive deploy() throws Exception {...} private static CyclicBarrier barrier; (1) private static File newFile; (1) private static int mode; (1) @Before public void init() throws Exception { newFile = null; mode = 0; barrier = new CyclicBarrier(2); (2) } @Test public void testTxtFile() throws Exception { File tempFile = new File(".", "testFile.txt"); assertTrue("Could not create temp file", tempFile.createNewFile()); barrier.await(10, TimeUnit.SECONDS); assertEquals(tempFile.getName(), newFile.getName()); assertEquals(FileEvent.CREATE, mode); } public void notifyFileEvent(@Observes FileEvent fileEvent) { mode = fileEvent.getMode(); newFile = fileEvent.getFile(); try { barrier.await(); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } } }