Hi Paul, Even when testing non-EO related stuff, I favor injecting objects manually instead of relying on an injector. That said, the solution to your problem is a little bit more complicated since you need the injected object during the EO initialization. I see two options:
Create a protected method to initialize the EO dependency: class MyEO { private AnyObject object; @Inject protected void initMyObject(AnyObject object) { this.object = object; } } @Before public void setup() { eo = new MyEO(); eo.initMyObject(anObject); editingContext.insertObject(eo); // or insertSavedObject if you want to create a dummy EO. } Alternatively, you can add a protected constructor to initialize the EO dependency: class MyEO extends ERXGenericRecord { @Inject private AnyObject myObject; protected MyEO(AnyObject object) { myObject = object; } } @Before public void setup() { eo = new MyEO(anObject); editingContext.insertObject(eo); // or insertSavedObject if you want to create a dummy EO. } Even though I prefer constructor injection for non-EO objects, I usually implement the method injection when testing EOs. It's not cool if you have to do it all the time, but it looks more polished than making your EO ask for a test version of the injected object. What do you think? Anyway, it would probably make sense to create a WOUnit extension to mitigate that problem. Cheers, HP > On Dec 4, 2017, at 12:09 AM, Paul Hoadley <pa...@logicsquad.net> wrote: > > Hello, > > I have an EO class that has an object provided by an @Inject annotation, and > it wants to use that object in init(). This works just fine in the context of > a running InjectableApplication. No matter what I try, though, I can’t get > the EO created in the context of a WOUnit test. What I’ve done in another > project is to manually create an Injector in a @Before method, and then call > injector.injectMembers() on the EO. But the EO needs to exist, and to create > it I need the injected object—chicken and egg. > > I’ve come up with a workaround (where init() asks for the object via an > accessor method which provides a “test version” of it if injection hasn’t > occurred), but it’s a bit of a hack. There must be a better way. Henrique, > are you around? > > > -- > Paul Hoadley > https://logicsquad.net/ <https://logicsquad.net/> > https://www.linkedin.com/company/logic-squad/ > >
_______________________________________________ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com