diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/beanvalidation/BeanValidationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/beanvalidation/BeanValidationTest.java index 170808bb77..e35caf1165 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/beanvalidation/BeanValidationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/beanvalidation/BeanValidationTest.java @@ -12,6 +12,7 @@ import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.RequiresDialect; import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import javax.persistence.RollbackException; @@ -31,14 +32,21 @@ import static org.junit.jupiter.api.Assertions.fail; ) public class BeanValidationTest { + @AfterEach + public void tearDown(EntityManagerFactoryScope scope){ + scope.inTransaction( + entityManager -> + entityManager.createQuery( "delete from CupHolder" ) + ); + } + @Test public void testBeanValidationIntegrationOnFlush(EntityManagerFactoryScope scope) { - scope.inEntityManager( + scope.inTransaction( entityManager -> { CupHolder ch = new CupHolder(); ch.setRadius( new BigDecimal( "12" ) ); ch.setTitle( "foo" ); - entityManager.getTransaction().begin(); try { entityManager.persist(ch); entityManager.flush(); @@ -51,41 +59,37 @@ public class BeanValidationTest { entityManager.getTransaction().getRollbackOnly(), "A constraint violation exception should mark the transaction for rollback" ); - entityManager.getTransaction().rollback(); - entityManager.clear(); } ); } @Test public void testBeanValidationIntegrationOnCommit(EntityManagerFactoryScope scope) { - scope.inEntityManager( - entityManager -> { - CupHolder ch = new CupHolder(); - ch.setRadius(new BigDecimal("9")); - ch.setTitle("foo"); - entityManager.getTransaction().begin(); - entityManager.persist(ch); - entityManager.flush(); - try { - ch.setRadius(new BigDecimal("12")); - entityManager.getTransaction().commit(); - fail("invalid object should not be persisted"); + try { + scope.inTransaction( + entityManager -> { + CupHolder ch = new CupHolder(); + ch.setRadius( new BigDecimal( "9" ) ); + ch.setTitle( "foo" ); + entityManager.persist( ch ); + entityManager.flush(); + + ch.setRadius( new BigDecimal( "12" ) ); } - catch (RollbackException e) { - final Throwable cve = e.getCause(); - assertTrue(cve instanceof ConstraintViolationException); - assertEquals(1, ((ConstraintViolationException) cve).getConstraintViolations().size()); - } - entityManager.close(); - } - ); + ); + fail( "invalid object should not be persisted" ); + } + catch (RollbackException e) { + final Throwable cve = e.getCause(); + assertTrue( cve instanceof ConstraintViolationException ); + assertEquals( 1, ( (ConstraintViolationException) cve ).getConstraintViolations().size() ); + } } @Test @RequiresDialect(H2Dialect.class) public void testTitleColumnHasExpectedLength(EntityManagerFactoryScope scope) { - scope.inEntityManager( + scope.inTransaction( entityManager -> { int len = (Integer) entityManager.createNativeQuery( "select CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS c where c.TABLE_NAME = 'CUPHOLDER' and c.COLUMN_NAME = 'TITLE'" diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/beanvalidation/ValidatorFactory2PhaseInjectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/beanvalidation/ValidatorFactory2PhaseInjectionTest.java index 7d0ffa5723..eaf3db0b58 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/beanvalidation/ValidatorFactory2PhaseInjectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/beanvalidation/ValidatorFactory2PhaseInjectionTest.java @@ -17,12 +17,14 @@ import org.hibernate.cfg.AvailableSettings; import org.hibernate.jpa.HibernatePersistenceProvider; import org.hibernate.jpa.boot.spi.Bootstrap; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; + +import org.hibernate.testing.orm.junit.BaseUnitTest; import org.hibernate.test.jpa.xml.versions.JpaXsdVersionsTest; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertSame; @@ -31,15 +33,17 @@ import static org.junit.jupiter.api.Assertions.assertSame; * * @author Steve Ebersole */ -public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase { + +@BaseUnitTest +public class ValidatorFactory2PhaseInjectionTest { private ValidatorFactory vf; - @Before + @BeforeEach public void before() { vf = Validation.byDefaultProvider().configure().buildValidatorFactory(); } - @After + @AfterEach public void after() { if ( vf != null ) { vf.close(); @@ -58,9 +62,9 @@ public class ValidatorFactory2PhaseInjectionTest extends BaseUnitTestCase { }, Collections.emptyMap() ); - emfb.withValidatorFactory( vf ); + emfb.withValidatorFactory( vf ); - EntityManagerFactory emf = emfb.build(); + EntityManagerFactory emf = emfb.build(); try { assertSame( vf, emf.getProperties().get( AvailableSettings.JPA_VALIDATION_FACTORY ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/boot/BootFailureTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/boot/BootFailureTest.java index 41a8ec4785..5751bd7994 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/boot/BootFailureTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/boot/BootFailureTest.java @@ -20,7 +20,8 @@ import org.hibernate.cfg.AvailableSettings; import org.hibernate.service.spi.ServiceException; import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * Test to verify that a dump configuration error results in an exception being @@ -31,14 +32,16 @@ import org.junit.Test; */ public class BootFailureTest { - @Test(expected = ServiceException.class) + @Test public void exceptionOnIllegalPUTest() { - bootstrapPersistenceUnit( "IntentionallyBroken" ); + Assertions.assertThrows( ServiceException.class, () -> + bootstrapPersistenceUnit( "IntentionallyBroken" ) ); } - @Test(expected = ServiceException.class) + @Test public void exceptionOnIllegalPUWithoutProviderTest() { - bootstrapPersistenceUnit( "IntentionallyBrokenWihoutExplicitProvider" ); + Assertions.assertThrows( ServiceException.class, () -> + bootstrapPersistenceUnit( "IntentionallyBrokenWihoutExplicitProvider" ) ); } private void bootstrapPersistenceUnit(final String puName) { diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/BaseUnitTest.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/BaseUnitTest.java new file mode 100644 index 0000000000..51bd334f54 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/BaseUnitTest.java @@ -0,0 +1,20 @@ +package org.hibernate.testing.orm.junit; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Inherited +@Target( ElementType.TYPE ) +@Retention( RetentionPolicy.RUNTIME ) + +@ExtendWith( FailureExpectedExtension.class ) +@ExtendWith( ExpectedExceptionExtension.class ) +@ExtendWith( DialectFilterExtension.class ) +public @interface BaseUnitTest { + +}