1

I have an interceptor that appears to be loaded correctly by JBoss (EAP 6.4) when I use it in a web app, but is rejected when I use the same beans.xml in a weld-se context.

package com.stackoverflow;

import javax.enterprise.context.Dependent;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@BindingAnnotation
@Dependent
public class InterceptorTest {

    @AroundInvoke
    public Object crypto(InvocationContext invocationContext) throws Exception {
        return null;
    }
}

And a Test:

package com.stackoverflow;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.Before;
import org.junit.Test;

public class InterceptorTestTest {

    WeldContainer container;

    @Before
    public void setup(){
        Weld weld = new Weld();
        container = weld.initialize();
    }

    @Test
    public void crypto() throws Exception {

    }

}

When I run the test:

[main] INFO org.jboss.weld.Version - WELD-000900 1.1.28 (Final)
[main] INFO org.jboss.weld.Bootstrap - WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.

org.jboss.weld.exceptions.DeploymentException: WELD-001417 Enabled interceptor class <class>com.stackoverflow.InterceptorTest</class> in file:/Users/pstanden/IdeaProjects/crypto/test/target/test-classes/META-INF/beans.xml@7 is neither annotated @Interceptor nor registered through a portable extension

    at org.jboss.weld.bootstrap.Validator.validateEnabledInterceptorClasses(Validator.java:503)
    at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:373)
    at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:379)
    at org.jboss.weld.bootstrap.api.helpers.ForwardingBootstrap.validateBeans(ForwardingBootstrap.java:85)

I can't really work out what I should be adding or removing, and why Weld org.jboss.weld.se:weld-se:1.1.28.Final seems to do different things to the same version of weld in JBoss?

EDIT: My beans.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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.stackoverflow.InterceptorTest</class>
    </interceptors>
</beans>

Edit again my pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>com.stackoverflow</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>test</artifactId>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se</artifactId>
            <version>1.1.28.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.javaee</groupId>
            <artifactId>javaee</artifactId>
            <version>6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>
  • There has to be some extra settings to yield you this error. Normally, Weld isn't strict about `beans.xml` verification and it only gives you warnings etc of something goes awry. Could you post your `beans.xml` too? – Siliarus May 11 '17 at 10:09
  • added to OQ, so all in one place – Phil Standen May 11 '17 at 10:31
  • I am puzzled, I just made a reproducer from what you posted and it work for me. Are you using the right artifact? E.g. for Weld SE in this (prehistoric) version you need `org.jboss.weld.se:weld-se:1.1.28.Final` – Siliarus May 11 '17 at 12:46
  • Yup, from my pom: org.jboss.weld.se weld-se 1.1.28.Final test I am using that version, since it matches the target app container Weld Version (JBoss EAP 6.4). – Phil Standen May 11 '17 at 12:50
  • Is it possible to send me your reproducer? I added my pom.xml, there are no other files in the project, at all. – Phil Standen May 11 '17 at 13:56

2 Answers2

1

Alright, managed to reproduce your problem and understood what you were doing. For the record it worked for me because I run it as a main method, not as a test.

The problem here is that you are using plain JUnit and your classes are in src/test - Weld won't pick them up, or rather, won't scan them and identify them as beans/interceptors etc. Therefore, your interceptor won't be found and the definition in beans.xml will be invalid leading to the exception you see.This is a known problem with JUnit as it is not sufficient to test CDI.

To workaround this, you can place these classes into src/main and they should be picked up correctly. But chances are, sooner or later you will bump into similar problems.

To truly resolve this, I suggest you twrite tests using Arquillian as a framework and Arquillian-container-Weld as a container in which it runs. Note that this is still SE (although Arquillian is used for EE testing too). It allows you to create a test archive and put inside whatever you deem worthy and Weld will pick it up all.

To inspire yourself, you can take a look at Weld testsuite, where the same thing is used in SE testing. Here is a link to one such test.

Siliarus
  • 6,393
  • 1
  • 14
  • 30
  • Thanks, Arquillian seems like it is the way to go. Unfortunately I have (4th attempt) never managed to get it to work (nothing to do with you answer with is the correct way to do it). – Phil Standen May 11 '17 at 19:31
  • Forgive my pessimism, seems I have over come this White Whale. – Phil Standen May 11 '17 at 19:46
  • @Phil Standen Yea it is very puzzling at first indeed (well, actually ever later on), but in the same time extremely powerful. If you have problems with that, you can check [Weld tips about testing](http://weld.cdi-spec.org/news/tags/tips/#_java_se_environment). If that doesn't help, reach out to [weld-dev mailing list](https://lists.jboss.org/mailman/listinfo/weld-dev) and I am sure we can find a minute to offer a helping hand. – Siliarus May 11 '17 at 20:12
1

To help, you can use :

this.weld = new Weld(); weld.addBeanClass(AInterceptor.class); weld.addBeanClass(BInterceptor.class); weld.enableInterceptors(AInterceptor.class,BInterceptor.class);

Sebastien DIAZ
  • 351
  • 3
  • 2