How to enable translation of Exception in Spring -Data when running from test?

I am using Spring -Data and want PersistenceExceptions be translated into Springs DataAccessExceptions .

I activated Spring-Data with @EnableJpaRepositories , and I see that org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0 loaded by Spring at startup. A PersistenceExceptionTranslator also available.

As I understand the documentation , the <jpa:repositories> namespace declaration activates Beans exception translation annotated using @Repository . @EnableJpaRepositories this also apply to Java configuration ( @EnableJpaRepositories )?

Does this mean that I should annotate my Spring -Data repository interface using @Repository or do I need to use Spring data as configured as above?

This is the interface:

 @Repository public interface DemoDao extends JpaRepository<Demo, Long> {} 

And this is the test:

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestDaoWithEmbeddedDatabaseConfiguration.class}) @Transactional public class DemoTest { @Test(expected = DataAccessException.class) public void testFindByTransactionStatus() throws Exception { persistDemoDataWithUniqueConstraintError(); // this calls DemoDao#save() } } 

This generates:

 javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException 

I cannot figure out how to enable exception translation. Any ideas?

Change 1:

This is a test configuration and Spring setup:

 @Configuration @ComponentScan("com.demo.dao") @EnableJpaRepositories("com.demo.dao") @EnableTransactionManagement public class TestDaoWithEmbeddedDatabaseConfiguration { // DataSource (EmbeddedDatabaseBuilder), EntityManagerFactory @Bean configuration @Bean public PersistenceExceptionTranslator persistenceExceptionTranslator() { return new HibernateExceptionTranslator(); } } 
+4
source share
1 answer

I found that the default priority swap job is used by default using Spring 4.1.5.RELEASE and Spring Data JPA 1.7.2.RELEASE.

Per PersistenceExceptionTranslationPostProcessor javadoc:

All Spring applicable resource plants (e.g. LocalContainerEntityManagerFactoryBean) implement the PersistenceExceptionTranslator Interface out of the box.

My configuration ...

 @Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef="emfb", ...) @ComponentScan(...) public class TestConfig { @Bean public LocalContainerEntityManagerFactoryBean emfb() { ... } ... } 

... and a JUnit4 test, for example ...

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={TestConfig.class}) public class MyTest { ... @Test public void expectFKViolation() { ... } ... } 

... leads to:

 Tests in error: expectFKViolation(...): ...; nested exception is org.hibernate.PropertyValueException 

Test output combination for org.hibernate.PropertyValueException :

 ostcsDirtiesContextTestExecutionListener - After test method: context [ ..., testException = org.springframework.dao.DataIntegrityViolationException: not-null ...; nested exception is org.hibernate.PropertyValueException ... ] 

Setting the translated type testException in the test method causes the test to go green:

 @Test(expected="org.springframework.dao.DataIntegrityViolationException.class") public void expectFKViolation() { ... } 
0
source

Source: https://habr.com/ru/post/1494929/


All Articles