Hi Henrique,

Thanks a lot for taking a look.

On 6 Dec 2017, at 1:13 am, Henrique Prange <hpra...@gmail.com> wrote:

> 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:

Just before we look at them, let me show you what I’ve got now.

public class MyEO {
        @Inject
        private AnyObject object;

        @Override
        protected void init(EOEditingContext ec) {
                setSomeAttribute(object.someValue());
                return;
        }
}

So that works as expected when running an InjectableApplication.

> 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.
> }

In this first solution, there’s no @Inject annotation on 'AnyObject object' 
now. Forgive my Guice ignorance here—does the @Inject on initMyObject(AnyObject 
object) cause that method to be _called_ by Guice? If so, when? (And if not, 
does this still work in the InjectableApplication setting?)

> 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.
> }

This looks good.

> Even though I prefer constructor injection for non-EO objects, I usually 
> implement the method injection when testing EOs.

Do you mean you prefer your first example above? I kind of liked the second one.

> 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?

Great solutions—thanks. I’ll test them out tomorrow.

> Anyway, it would probably make sense to create a WOUnit extension to mitigate 
> that problem.

Tell me more about this. My first attempt was to pilfer some of the stuff in 
WOInject’s src/test/java, but I never quite made anything work. What would a 
WOUnit extension do here (and how can I help)? (Also, I saw some recent commits 
to WOUnit—you planning a new release?)


-- 
Paul Hoadley
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

Reply via email to