Does CDI-Unit support interceptors?

32 views
Skip to first unread message

da...@nikio.io

unread,
Oct 19, 2018, 4:33:15 AM10/19/18
to CDI-Unit
I have a permission interceptor:

@InterceptorBinding
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Permission {
@Nonbinding Competence value() default Competence.NONE;
}

and the corresponding implementation (implementation not complete ;-) ):

@Permission
@Interceptor
public class PermissionInterceptor {

@AroundInvoke
public Object permissionMethod(InvocationContext ctx) throws Exception {
if(hasNoPermission()){
throw new NoPermissionException();
}
Object result = ctx.proceed();
return result;
}

private boolean hasNoPermission() {
return true;
}
}

I also registered it my beans.xml. When i use it via CDI-Runner I don`t get in the interceptor method. So my question is: Does cdi-unit supports this kind of interceptors?

Lars-Fredrik Smedberg

unread,
Oct 19, 2018, 4:51:34 AM10/19/18
to cdi-...@googlegroups.com
Hi

You need to make the Interceptor visible to CDI-Unit like for any other classes using e.g. @AdditionalClasses

As far as I know it does not make use of beans.xml and I'm not sure if it supports globally activated interceptors.

Best regards
LF

--
You received this message because you are subscribed to the Google Groups "CDI-Unit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cdi-unit+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Med vänlig hälsning / Best regards

Lars-Fredrik Smedberg

STATEMENT OF CONFIDENTIALITY:
The information contained in this electronic message and any
attachments to this message are intended for the exclusive use of the
address(es) and may contain confidential or privileged information. If
you are not the intended recipient, please notify Lars-Fredrik Smedberg
immediately at itsm...@gmail.com, and destroy all copies of this
message and any attachments.

da...@nikio.io

unread,
Oct 19, 2018, 5:21:23 AM10/19/18
to CDI-Unit
Do I need the annotation on my PermissionInterceptor class or in my unit test? Because I already tried to use it in unit-test and it doesnt work. I used

@AdditionalClasses(PermissionInterceptor.class)

on my unit-test.

Lars-Fredrik Smedberg

unread,
Oct 19, 2018, 5:22:58 AM10/19/18
to cdi-...@googlegroups.com
In the unit test but I think you should include the implementation class and not the interceptor binding

Regards
LF

--
You received this message because you are subscribed to the Google Groups "CDI-Unit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cdi-unit+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

da...@nikio.io

unread,
Oct 19, 2018, 5:30:01 AM10/19/18
to CDI-Unit
I tried this one: 

@RunWith(CdiRunner.class)
@AdditionalClasses(PermissionInterceptor.class)
public class MyUnitTest{

but it does not work. When I use the PermissionInterceptor in beans.xml it works, so I think this additional class should be right. It is a little bit strange.

Am Freitag, 19. Oktober 2018 10:33:15 UTC+2 schrieb da...@nikio.io:

da...@nikio.io

unread,
Oct 22, 2018, 4:07:53 AM10/22/18
to CDI-Unit
I solved the problem. It was a mixture with mockito mocks and the annotation @injectMocks which produces the problems. Thanks for your help.


Am Freitag, 19. Oktober 2018 10:33:15 UTC+2 schrieb da...@nikio.io:
Reply all
Reply to author
Forward
0 new messages