Using hibernate validator with tomee-embedded

Egor Duda <[email protected]> Mon, 8 Apr 2024 16:22:13 +0300
Newsgroups gmane.comp.java.openejb.user
Message-ID <8df71eb3-ce27-47bd-889a-fe9180645fc3__29232.4183321156$1712582549$gmane$org@gmail.com>
Hello!

I'm trying to use tomee-embedded for unit testing EJBs and some of those EJBs functions make use of hibernate-specific 
validators, such as @ISBN. Running ISBNTest from the code below causes
jakarta.validation.UnexpectedTypeException: No compliant org.hibernate.validator.constraints.ISBN ConstraintValidator 
found for annotated element of type java.lang.String

As I understand, validation is being performed by apache bval, which is integral part of tomee-embedded, and bval lacks 
@ISBN validator. Is there any way to override validator implementation inside tomee-embedded instance to use hibernate 
validator instead of bval?

Thanks in advance!

Sample code below:
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
     <modelVersion>4.0.0</modelVersion>

     <groupId>test</groupId>
     <artifactId>ISBNTest</artifactId>
     <version>1.0-SNAPSHOT</version>

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

     <dependencies>
         <dependency>
             <groupId>org.apache.tomee</groupId>
             <artifactId>tomee-embedded</artifactId>
             <version>9.1.2</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>jakarta.platform</groupId>
             <artifactId>jakarta.jakartaee-api</artifactId>
             <version>9.1.0</version>
         </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.13.2</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.hibernate.validator</groupId>
             <artifactId>hibernate-validator</artifactId>
             <version>8.0.1.Final</version>
         </dependency>
     </dependencies>
</project>

src/main/java/test/ISBNService.java:

package test;

import jakarta.ejb.Stateless;
import org.hibernate.validator.constraints.ISBN;

@Stateless
public class ISBNService {
     public void process(@ISBN String isbn) {
         // do nothing in particular
     }

}

src/test/java/test/ISBNTest.java:

package test;

import jakarta.ejb.embeddable.EJBContainer;
import java.util.Properties;
import javax.naming.NamingException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class ISBNTest {

     private static EJBContainer container;

     private ISBNService ISBNService;

     @BeforeClass
     public static void init() {
         Properties properties = new Properties();
         properties.setProperty(EJBContainer.PROVIDER, "tomee-embedded");
         container = EJBContainer.createEJBContainer(properties);
     }

     @Before
     public void inject() throws NamingException {
         ISBNService = (ISBNService) container.getContext().lookup("global/ISBNTest/ISBNService");
     }

     @Test
     public void isbnTest() {
         ISBNService.process("123");
     }

}