Use arquillian to test Interceptor
wguo Sep 22, 2014 4:35 AMHi ,
when i want to test Interceptor using arquillian , i found it never invoke the aroundInvoke method , any advice
1) . The interceptor method :
@Interceptor @Cached public class CacheInterceptor implements Serializable { private static final long serialVersionUID = 1L; private static final Logger LOGGER = LoggerFactory.getLogger(CacheInterceptor.class); private Cache<Object, Optional<Object>> cache = null; @AroundInvoke public Object cache(final InvocationContext ctx) throws Exception { try { //do sth } finally { LOGGER.trace(cache.stats().toString()); } } // Get a Cache for the given invocation, possibly constructing one private Cache<Object, Optional<Object>> getCache(InvocationContext ctx) {
@Inherited @InterceptorBinding @Retention(RUNTIME) @Target({METHOD, TYPE}) public @interface Cached { @Nonbinding long maximumSize() default 1000; /* 1h */ @Nonbinding long expireDuration() default 60 * 60; @Nonbinding TimeUnit expireTimeUnit() default TimeUnit.SECONDS; @Nonbinding boolean recordStats() default true; }
3. Test
public class CacheTest extends Arquillian { @Deployment public static Archive<?> createDeployment() { return ShrinkWrap .create(WebArchive.class) .addClass(Cached.class) .addClass(CacheInterceptor.class) .addAsManifestResource( new FileAsset(new File( "src/test/resources/META-INF/cache_beans.xml")), "beans.xml"); } @Test public void testMethod() { for (int i = 0; i < 3; i++) { getRealName("test"); } System.out.println("testMethod"); } @Cached public String getRealName(String name) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Real " + name); return "Real " + name; } }
4 .
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> <interceptors> <class>com.test.cache.CacheInterceptor</class> </interceptors> </beans>