此内容没有您所选择的语言版本。
Chapter 5. Applying interceptors to tests
Quarkus tests are full CDI beans, so you can apply CDI interceptors as you would normally. For example, if you want a test method to run within the context of a transaction, you can apply the @Transactional
annotation to the method. You can also create your own test stereotypes.
Procedure
Add the
quarkus-narayana-jta
dependency to yourpom.xml
file:<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-narayana-jta</artifactId> </dependency>
Make sure the
TransactionalQuarkusTest.java
includes the following import statements:package org.acme.quickstart; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.enterprise.inject.Stereotype; import javax.transaction.Transactional; import io.quarkus.test.junit.QuarkusTest;
Create the
@TransactionalQuarkusTest
annotation:@QuarkusTest @Stereotype @Transactional @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface TransactionalQuarkusTest { }
Apply this annotation to a test class where it will behave as if you applied both the
@QuarkusTest
and@Transactional
annotations:@TransactionalQuarkusTest public class TestStereotypeTestCase { @Inject UserTransaction userTransaction; @Test public void testUserTransaction() throws Exception { Assertions.assertEquals(Status.STATUS_ACTIVE, userTransaction.getStatus()); } }
This is a simple test that evaluates the greeting service directly without using HTTP.