0

I'm using a Deltaspike (1.4.0) with Quartz (2.2.1) to schedule a job. In the TestJob bean is injected ResourceBundle. The producer of ResourceBundle needs a facesContext, but this always is null.

How can inject properly ResourceBundle in the scheduler bean and why facesContext is always null when is used @Scheduled?

@Scheduled(cronExpression = "0 0/1 * * * ?")
public class TestJob implements Job {

    @Inject private EntityManager em;
    @Inject private transient ResourceBundle i18n;

    public TestJob() {}

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("TestJob executed...");
    }
}

ResourceBundle producer

public class ResourceBundleProducer implements Serializable {
    @Inject public FacesContext facesContext;

    @Produces
    public ResourceBundle getResourceBundle() {
       if (facesContext.getViewRoot() != null) 
          return ResourceBundle.getBundle("i18n.i18n", facesContext.getViewRoot().getLocale());
       else
          return ResourceBundle.getBundle("i18n.i18n", facesContext.getApplication().getViewHandler().calculateLocale(facesContext));
    }
}

FacesContext producer

public class FacesContextProducer implements Serializable {
    @Produces
    @RequestScoped
    public FacesContext produceFacesContext() {
        return FacesContext.getCurrentInstance();
    }
}
Tvori
  • 298
  • 8
  • 19

1 Answers1

0

You need to mock the JSF-container which is supported with some additional config.

See e.g. http://deltaspike.apache.org/documentation/test-control.html#JSF(viaMyFaces-Test)

Also see the example provided by OS890: https://github.com/os890/ee6-ds-demo/blob/master/src/test/java/org/os890/demo/ee6/test/PageBeanTest.java

Or you provide a specialized bean (see @Specializes) of your ResourceBundleProducer in the test-classpath and use a logic which isn't based on the FacesContext.

If you ask such questions on their mailing-list, you get answers petty quickly.

Dar Whi
  • 822
  • 5
  • 14