You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
| Online: | 13 Members of 9199 |
| Forum: Seam Users |
18. Mar 2010, 14:29 America/New_York | Link |
Hi,
I want to test an application scoped component using SeamTest. I cannot use a plain unit test since I need some values like the current viewID. How can I destroy/remove my component and recreate a new one?
Here is what I'm trying to achieve:
@BeforeMethod
public void setUp()
throws Exception
{
new FacesRequestLocal()
{
@Override
protected void invokeApplication()
throws Exception
{
MyApplicationScopedBean oldBean = (MyApplicationScopedBean) Component.getInstance("bean");
// ********** I tried that:
// oldBean.destroy();
// Contexts.removeFromAllContexts("bean");
// Component.forName("bean").destroy(oldBean);
MyApplicationScopedBean newBean = (MyApplicationScopedBean) Component.getInstance("bean");
// ********* false
assertThat(newBean).isNotSameAs(oldBean);
}
}.run();
}
The component is stored in the ELContext and I don't know how to remove it.
Thanks, David
Something like this should do. I do not have IDE around now, so i'm writting off the top of my head.
Contexts.getApplicationContext().remove("bean");Thank you for your answer but I have already tried this, in fact I replaced it with the more general , but no luck so far.
Well, I tried the following (from org.jboss.seam.contexts.Contexts.destroy) which is called when the application is shut down but it does not work:
Component component = Component.forName("my.full.bean"); Object object = Contexts.getApplicationContext().get("bean"); component.destroy(object); Contexts.removeFromAllContexts("bean");So guys how do you test your application scopped components ???
I found this that seems to do the job:
/** * Creates a new instance of the specified component and registers it in its scope. * * @param <T> the class of the component. * @param clazz the class of the component. * @param aliases the factory aliases of the component. * * @throws Exception if an error occurs. */ @SuppressWarnings("unchecked") protected <T> void newInstance(final Class<T> clazz, final String... aliases) throws Exception { new FacesRequestEx() { @Override protected void invokeApplication() { final String componentName = Component.getComponentName(clazz); final Component component = Component.forName(componentName); final Context componentContext = component.getScope().getContext(); final T result = (T) component.newInstance(); componentContext.set(componentName, result); for (String alias : aliases) { componentContext.set(alias, result); } } }.run(); }I just don't believe that does not work. I have no problem with testing application scoped components.
What is your problem with viewId ? It is always provided if you use the Faces/NonFacesRequest. Besides, in my opinion, the application scoped components should be designed in such a way that they could have without knowledge about views.