Hello Mark,

thank you for your input. Your idea would have been much easier than what I did 
:-)

When you write a Junit Runner, you have the ability to control the test class 
creation by overwriting createTest() and returning your instance. By default 
this is simply the newInstance() call for the constructor with no arguments of 
the test class. The following code will therefore not only provide injection 
but even create a fully creational contexted bean that even will trigger 
interceptors. I've made sure though, that after calling all 
'AfterClass'-Annotations, the creational context is released too, so the bean 
does not stick around.

I do not really have a clue though how this could be done with TestNg.

I might miss out some clearContexts() here. I thought about cleaning the 
contexts before a new test-method is run, however I loose all beans discovered 
and created while the container booted, and that lead to failing tests. I need 
the container being created BEFORE createTest() is called to be able to do the 
bean creation. I have to dive into this more deeply. Another Idea would be to 
simply boot the container again before each method and shut it down afterwards, 
though this will slow down tests.

public class TestRunner extends BlockJUnit4ClassRunner
{
    private Class<?> clazz;
    private CreationalContext<?> creationalContext;

    public TestRunner(final Class<?> clazz) throws InitializationError
    {
        super(clazz);

        this.clazz = clazz;

        cdiContainer = CdiContainerLoader.getCdiContainer();
        cdiContainer.boot();
        cdiContainer.getContextControl().startContexts();
    }

    @Override
    protected Object createTest()
        throws Exception
    {
        return createTest(clazz);
    }

    @SuppressWarnings("unchecked")
    private <T> T createTest(final Class<T> testClass) throws Exception
    {
        final BeanManager beanManager = cdiContainer.getBeanManager();

        final Set<Bean<?>> beans = beanManager.getBeans(testClass);

        assert !beans.isEmpty();

        final Bean<?> bean = beans.iterator().next();
        creationalContext = beanManager.createCreationalContext(bean);

        final T result = (T) beanManager.getReference(bean, testClass, 
creationalContext);

        return result;
    }

    @Override
    protected Statement withAfterClasses(final Statement statement)
    {
        final Statement defaultStatement = super.withAfterClasses(statement);

        return new Statement()
        {

            @Override
            public void evaluate()
                throws Throwable
            {
                try
                {
                    defaultStatement.evaluate();
                }
                finally
                {
                    creationalContext.release();
                }
            }

        };
    }

Regards,

Heiko

> -----Ursprüngliche Nachricht-----
> Von: Mark Struberg [mailto:strub...@yahoo.de]
> Gesendet: Freitag, 7. Februar 2014 09:29
> An: dev@deltaspike.apache.org
> Betreff: Re: AW: Interceptors not being called in JUnit-aware CDI environment
>
> =========================================================
> ======
>
> ATTENTION! This message contains suspicious URL(s) possibly redirecting to
> malicious content. Our security gateways target known problem URLs like
> freeweb or URL Shorteners that are being abused by spammers. Some
> examples would be groups.google.com or tinyurl.com. Please check the sender
> and hyperlinks in the e-mail accurately before clicking on a link. If this 
> email
> seems obviously bad to you please delete it.  More information is available on
> our website Information Security @ Daimler: http://intra.corpintra.net/intra-
> is-e/spam
>
> =========================================================
> ======
>
> ACHTUNG! Diese E-Mail enthält verdächtige URLs welche möglicherweise auf
> schädlichen Inhalt verweisen. Die Security Gateways prüfen auf bekannte
> Problem-URLs  wie zum Beispiel URL-Abkürzungen, die bevorzugt von
> Spammern mißbraucht werden (tinyurl.com, groups.google.com, ...). Bitte
> prüfen Sie den Absender und die URLs in dieser E-Mail gewissenhaft bevor sie
> die verknüpften Inhalte aufrufen. Bitte löschen Sie diese E-Mail, wenn Sie der
> Meinung sind, daß sich der Verdacht bestätigt. Weitere Informationen zu
> unerwünschter E-Mail / SPAM finden Sie auf den Seiten der
> Informationssicherheit bei Daimler unter: http://intra.corpintra.net/intra-is-
> d/spam
>
> =========================================================
> ======
>
>
> Hi Heiko!
>
> Afaik all unit runners (junit, testng) do create and manage the test class
> instances themselfs. We only do injection into those existing instances. With
> other words: we just fill the injection points. Whenever I need to have an
> intercepted method, I create a public static inner class in my test. E.g. 
> when I
> need a @Transactional DbHelper with a cleanup() method which should run in
> an own transaction.
>
> private @Inject DbHelper dbHelper;
>
> pubic static class DbHelper {
>   private @Inject EntityManager em;
>    @Transactional
>    public void cleanupDb() {
>      em.....
>   }
> }
>
> @Before
> public void doCleanup() {
>   cleanInstances() // restart session and request scoped context
>   dbHelper.cleanupDb();
>
>   cleanInstances() // again to ensure the test has clean instances and the em
> got closed
>
> }
>
> LieGrue,
> strub
>
>
>
>
> On Friday, 7 February 2014, 9:13, "it-media.k...@daimler.com" <it-
> media.k...@daimler.com> wrote:
>
> Hey Romain,
> >
> >that was the missing piece of the puzzle and lets me step down in
> embarrassment for such an idiotic mistake. I've had the assumtion that for a
> maven surefire test, the beans.xml provided in src/main/resources/META-INF
> which IS obviously used, is enough, but after you pointed this out, I simply
> added another one under src/test/resources/META-INF and voilá, getBeans()
> returns a Bean that I can retrieve an instance from, that indeed will make
> interceptors work.
> >
> >Thank you very much for your help,
> >
> >Heiko
> >
> >> -----Ursprüngliche Nachricht-----
> >> Von: Romain Manni-Bucau [mailto:rmannibu...@gmail.com]
> >> Gesendet: Freitag, 7. Februar 2014 08:55
> >> An: dev@deltaspike.apache.org
> >> Betreff: Re: Interceptors not being called in JUnit-aware CDI
> >> environment
> >>
> >>
> =========================================================
> >> ======
> >>
> >> ATTENTION! This message contains suspicious URL(s) possibly
> >> redirecting to malicious content. Our security gateways target known
> >> problem URLs like freeweb or URL Shorteners that are being abused by
> >> spammers. Some examples would be groups.google.com or tinyurl.com.
> >> Please check the sender and hyperlinks in the e-mail accurately
> >> before clicking on a link. If this email seems obviously bad to you
> >> please delete it.  More information is available on our website
> >> Information Security @ Daimler: http://intra.corpintra.net/intra-
> >> is-e/spam
> >>
> >>
> =========================================================
> >> ======
> >>
> >> ACHTUNG! Diese E-Mail enthält verdächtige URLs welche möglicherweise
> >> auf schädlichen Inhalt verweisen. Die Security Gateways prüfen auf
> >> bekannte Problem-URLs  wie zum Beispiel URL-Abkürzungen, die
> >> bevorzugt von Spammern mißbraucht werden (tinyurl.com,
> >> groups.google.com, ...). Bitte prüfen Sie den Absender und die URLs
> >> in dieser E-Mail gewissenhaft bevor sie die verknüpften Inhalte
> >> aufrufen. Bitte löschen Sie diese E-Mail, wenn Sie der Meinung sind,
> >> daß sich der Verdacht bestätigt. Weitere Informationen zu
> >> unerwünschter E-Mail / SPAM finden Sie auf den Seiten der
> >> Informationssicherheit bei Daimler unter:
> >> http://intra.corpintra.net/intra-is-
> >> d/spam
> >>
> >>
> =========================================================
> >> ======
> >>
> >>
> >> is you test class scanned by cdi? I mean did you provide a META-
> >> INF/beans.xml for tests?
> >> Romain Manni-Bucau
> >> Twitter: @rmannibucau
> >> Blog: http://rmannibucau.wordpress.com/
> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> >> Github: https://github.com/rmannibucau
> >>
> >>
> >>
> >> 2014-02-07  <it-media.k...@daimler.com>:
> >> > Hello Romain,
> >> >
> >> > thank you for your fast response. Yes I want to intercept the test 
> >> > itself.
> >> >
> >> > I've checked into various ways on how to actually create a bean,
> >> > however
> >> nothing worked out. You mention to call beanManager.getXXX() but for
> >> my point that is not enough information.
> >> >
> >> > I tried to retrieve the beans of the test class via
> >> > beanManager.getBeans() but
> >> regardless of what I try as annotation literals there (non, Any,
> >> etc.) nothing works, the list is always empty. What method exactly
> >> did you have in mind here?
> >> >
> >> > Thanks,
> >> >
> >> > Heiko
> >> >
> >> >> -----Ursprüngliche Nachricht-----
> >> >> Von: Romain Manni-Bucau [mailto:rmannibu...@gmail.com]
> >> >> Gesendet: Freitag, 7. Februar 2014 08:26
> >> >> An: dev@deltaspike.apache.org
> >> >> Betreff: Re: Interceptors not being called in JUnit-aware CDI
> >> >> environment
> >> >>
> >> >>
> >>
> =========================================================
> >> >> ======
> >> >>
> >> >> ATTENTION! This message contains suspicious URL(s) possibly
> >> >> redirecting to malicious content. Our security gateways target
> >> >> known problem URLs like freeweb or URL Shorteners that are being
> >> >> abused by spammers. Some examples would be groups.google.com or
> tinyurl.com.
> >> >> Please check the sender and hyperlinks in the e-mail accurately
> >> >> before clicking on a link. If this email seems obviously bad to
> >> >> you please delete it.  More information is available on our
> >> >> website Information Security @ Daimler:
> >> >> http://intra.corpintra.net/intra- is-e/spam
> >> >>
> >> >>
> >>
> =========================================================
> >> >> ======
> >> >>
> >> >> ACHTUNG! Diese E-Mail enthält verdächtige URLs welche
> >> >> möglicherweise auf schädlichen Inhalt verweisen. Die Security
> >> >> Gateways prüfen auf bekannte Problem-URLs  wie zum Beispiel
> >> >> URL-Abkürzungen, die bevorzugt von Spammern mißbraucht werden
> >> >> (tinyurl.com, groups.google.com, ...). Bitte prüfen Sie den
> >> >> Absender und die URLs in dieser E-Mail gewissenhaft bevor sie die
> >> >> verknüpften Inhalte aufrufen. Bitte löschen Sie diese E-Mail, wenn
> >> >> Sie der Meinung sind, daß sich der Verdacht bestätigt. Weitere
> >> >> Informationen zu unerwünschter E-Mail / SPAM finden Sie auf den
> >> >> Seiten der Informationssicherheit bei Daimler unter:
> >> >> http://intra.corpintra.net/intra-is-
> >> >> d/spam
> >> >>
> >> >>
> >>
> =========================================================
> >> >> ======
> >> >>
> >> >>
> >> >> HI
> >> >>
> >> >> You want to intercept the test? so it means the runner needs to
> >> >> invoke "business" methods of the test class which is not the case
> >> >> by default (ie result shouldn't be built from a newInstance() but
> >> >> from a
> >> beanManager.getXXX()).
> >> >> Romain Manni-Bucau
> >> >> Twitter: @rmannibucau
> >> >> Blog: http://rmannibucau.wordpress.com/
> >> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> >> >> Github: https://github.com/rmannibucau
> >> >>
> >> >>
> >> >>
> >> >> 2014-02-07  <it-media.k...@daimler.com>:
> >> >> > Hello,
> >> >> >
> >> >> > I've been trying to start the CDI container during Junit-Testing
> >> >> > as described at the following page,
> >> >> >
> >> >> > http://struberg.wordpress.com/2012/03/27/unit-testing-
> strategies
> >> >> > -fo
> >> >> > r-c
> >> >> > di-based-projects/
> >> >> >
> >> >> > though I'm using Junit 4 and implemented everything using a new
> >> >> > Runner (to
> >> >> annotate tests with @RunWith). I kind of got inspired by he
> >> >> CDIUnit project, but it works only with Weld and I want to use
> OpenWebBeans.
> >> >> > The major things like injection work perfectly, however, I
> >> >> > realized, that even
> >> >> though I implemented the following code to provided a CDI aware
> >> >> Test-Class, an interceptor annotation on a test method is
> >> >> completely ignored, thus the interceptors AroundInvoke annotated
> method is not called.
> >> >> >
> >> >> >     private <T> T createTest(final Class<T> testClass) throws
> >> >> >Exception
> >> >> >     {
> >> >> >         final BeanManager beanManager =
> >> >> >cdiContainer.getBeanManager();
> >> >> >
> >> >> >         final CreationalContext<T> creationalContext =
> >> >> >beanManager.createCreationalContext(null);
> >> >> >
> >> >> >         final AnnotatedType<T> annotatedType =
> >> >> beanManager.createAnnotatedType(testClass);
> >> >> >         final InjectionTarget<T> injectionTarget =
> >> >> >beanManager.createInjectionTarget(annotatedType);
> >> >> >
> >> >> >         final T result = (T)
> >> >> > getTestClass().getOnlyConstructor().newInstance();
> >> >> >
> >> >> >         injectionTarget.inject(result, creationalContext);
> >> >> >
> >> >> >         return result;
> >> >> >     }
> >> >> >
> >> >> > Is this expected behaviour? I'm not so familiar with how
> >> >> > interceptors are really implemented, but I would have guest that
> >> >> > after providing a creational context and complete injection for
> >> >> > the test class, interceptor annotations should work too. The
> >> >> > interceptor itself is registered correctly. It is listed among
> >> >> > all interceptors when calling
> >> >> >
> >> >> > WebBeansContext.currentInstance().getInterceptorsManager().getCd
> >> >> > iIn
> >> >> > ter
> >> >> > ceptors()
> >> >> >
> >> >> > however, a test annotated with the interceptor does not trigger
> >> >> > execution of
> >> >> the AroundInvoke-method.
> >> >> >
> >> >> > Would be great, if somebody has a thought or a hint on this.
> >> >> >
> >> >> > Thanks,
> >> >> >
> >> >> > Heiko
> >> >> >
> >> >> >
> >> >> > If you are not the addressee, please inform us immediately that
> >> >> > you have
> >> >> received this e-mail by mistake, and delete it. We thank you for
> >> >> your
> >> support.
> >
> >> >> >
> >> >
> >> > If you are not the addressee, please inform us immediately that you
> >> > have
> >> received this e-mail by mistake, and delete it. We thank you for your
> support.
> >> >
> >
> >If you are not the addressee, please inform us immediately that you have
> received this e-mail by mistake, and delete it. We thank you for your support.
> >
> >
> >

If you are not the addressee, please inform us immediately that you have 
received this e-mail by mistake, and delete it. We thank you for your support.

Reply via email to