How to inject managed bean into FacesContext?

3k Views Asked by At

How can I replace an @ApplicationScoped managed bean with my own copy of it in FacesContext? All I have is an instance of FacesContext (I'm inside JSFUnit).

1

There are 1 best solutions below

2
On BEST ANSWER

Application scoped beans are stored in the application map with the managed bean name as key.

So, this should do:

FacesContext.getCurrentInstance().getExternalContext()
    .getApplicationMap().put("managedBeanName", new Bean());

By the way, deeper under the JSF covers up to the Servlet API, the application map is just a mapping of ServletContext attributes. Useful to know when it happens that you've only the ServletContext at your hands. And in the same line, the session map maps to HttpSession attributes and the request map to HttpServletRequest attributes. Use them for session and request scoped beans respectively.