I'm injecting SessionContext as a resource to an EJB (implementing container managed transactions):
@Stateless(name = "XXX", mappedName = "PPP-MMM-CCC")
@TransactionManagement(value=TransactionManagementType.CONTAINER)
public class Xxx implements ItsRemoteInterface {
@Resource
private SessionContext sctx;
....
}
My unit tests are failing because "sctx" is null while they're running (NullPointerException). So the only way I've figured to fix this is to create a FakeSessionContext class that implements SessionContext which then I can use during the test.
instance = new Xxx();
sessionContextResourceField = Xxx.class.getDeclaredField("sctx");
sessionContextResourceField.setAccessible(true);
sessionContextResourceField.set(instance, new FakeInitialContext());
But before I do that, I was wondering if there is a more elegant way? Other than creating a FakeSessionContext class? Something like a factory class?
In case of interest, I'm using jUnit 4.10 and jmockit 0.999.15.