Hi all,
Jozef and I just had a conversation about how to do this, and I
thought it was worth summarizing the discussion for everyone, as it's
not always obvious what the right approach is.
This is on the wiki
http://www.seamframework.org/WebBeans/TestingNoncontextualInstances
Firstly, what are non-contextual instances?
Non-contextual instances are those objects which aren't managed by
299. For example, a Wicket component is managed by Wicket (it's
lifecycle is controlled by Wicket for rendering a page), or an MDB is
managed by the EJB container (instantiated or retrieved from the pool
for message delivery). In other words, they are never stored in 299
contexts.
However we still want to be able to apply 299 services to it (like
injection, callbacks etc.).
The spec defines the Java EE component classes to be non-contextual
instances; custom non-contextual instances may be defined, such as
Wicket components.
The Java EE component classes which are currently:
* Servlet - servlets, servlet filters, event listeners
* JSP tag handlers, tag library event listeners
* JSF scoped managed beans
* JAX-WS service endpoints, handlers
* EJB beans, interceptors
* Java EE platform main class (static)
* login callback handler
How do we go about testing these?
1) have the EE container to instantiate the object in whatever way it
normally would (e.g. servlet via web request, MDB via resource
adaptor, WS via WS call etc.)
2) if necessary cause a method to be called on the object (e.g.
deliver a message to the MDB)
3) check whether the required injection, callback etc. occurs (is the
value what we expect) in that method and set a flag showing that the
object is in the correct state. In general you can do this by
injecting an application scoped managed bean, and setting the flag on
that. This can then be got inside e.g. the test case and it's value
queried. Alternatively, if you are testing something that supports
returning some output (e.g. for http request the http status code
works well), you can use this to indicate the status to the test.
Any examples?
We have some similar tests - for example verifying the state of
contexts in servlet service methods. Take a look at
org.jboss.jsr299.tck.tests.context.application.ServiceMethodServlet,
and the corresponding test
org.jboss.jsr299.tck.tests.context.application.ApplicationContextTest
which uses the second pattern described above.
Often these tests are hard to write, as they require you to understand
not just 299, but other bits of Java EE, and how they inter-relate.
Ask in the IRC channel or here if you are stuck - likelihood is that
someone has experience of this area of EE.