EntityManagerFactory Bean in Spring-boot test
@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "\\your package here" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("\\Driver");
dataSource.setUrl("\\URL");
dataSource.setUsername( "\\userName" );
dataSource.setPassword( "\\password" );
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
Remi
Updated on June 17, 2022Comments
-
Remi almost 2 years
I am new to the programming world so what I say may seem silly.
I am trying to run a spring-boot test as JUnit under Eclipse but I just can't figure out how to use the spring-boot annotations... I have read several guides and browsed this website but didn't find anything that resolved my problem.
I am trying to run the JUnit test-class below :
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={CBusiness.class,CService.class,CDao.class}, loader = AnnotationConfigContextLoader.class) @SpringBootTest public class CalculTest { @Autowired CBusiness business; @Test public void testCalcul() throws TechnicalException { Object object= new Object(); object.setId1("00"); object.setId2("01"); object.setNombrePlacesMaximum(new BigInteger("50")); Long result=business.calcul(object); assertTrue(result>0); }
Running this as a JUnit test gives me the following exception :
java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
The EntityManager parameter from the CDao class has the annotation @PersistenceContext, I thought this meant it was automatically generated by Hibernate but apparently it isn't... How can I instanciate the EntityManager using only java code? I don't have any .xml or .properties file...
FYI here are the classes called by the test :
Business Layer :
@Component("cBusiness") public class CBusiness { @Autowired CService cService; public long calcul(Object object) throws TechnicalException { //Code (calls a method from CService class) }
Service layer :
@Service public class CService { @Autowired CDao cDao;
Dao Layer
@Repository @Transactional(rollbackFor = {TechnicalException.class}) public class CDao { @PersistenceContext EntityManager entityManager;
I tried testing the method inside a webservice using only the @autowire annotation on the Business layer and if worked fine, however I just cannot instanciate it in the JUnit tests. I tried several ways of running this test and I am not sure this is the right way of doing it, so I'm open to any suggestion.
Thanks in advance.
-
Remi almost 7 yearsThis looks really complicated for my basic level of programming. Nevertheless I tried it, but I have no idea of what my driverClassName is and therefore I have a "cannot instantiate Bean DataSource" exception". Isn't there a way to let Hibernate create the EntityManagerFactory and not doing it myself? Once again my methods work just fine if I test it elsewhere than in a JUnit spring-boot test...
-
Binu almost 7 yearsTake a look into this. baeldung.com/the-persistence-layer-with-spring-and-jpa
-
M. Deinum almost 7 yearsWith spring boot you don't need this. Spring Boot does that for you out-of-the-box.