How to get a Spring IoC Bean using JSFUnit on JSF 1.2

93 Views Asked by At

So my application runs on JSF 1.2 and uses the Spring Beans IOC to inject classes into Backing Beans based on the Spring Context XML configuration.

I am using JSFUnit for testing, and I can navigate through pages and assert on my Backing Bean values, however I am unable yet to access any of the injected beans.

I know JSFUnit has been advertized to do white-box testing, but so far I could only do stuff similar to the in-container testing made with Arquilian.

Can anyone help me with accessing the Business Object I have (injected by Spring) once I open the page related to the Backing Bean ? Thanks !

1

There are 1 best solutions below

0
abdelrahman-sinno On

I found the answer, once you navigate to your page, you can call this method in your test case which simply returns the bean by bean name, it's so neat:

@SuppressWarnings("unchecked")
public static <T> T findBean(String beanName) {
    FacesContext context = FacesContext.getCurrentInstance();
    return (T) context.getApplication().evaluateExpressionGet(context, "#{"+beanName+"}", Object.class);
}

Now JSFUnit became much more useful !