Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityManagerFactory Bean in Spring-boot test

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.

like image 988
Remi Avatar asked May 23 '17 15:05

Remi


1 Answers

@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;
   }
like image 120
Binu Avatar answered Sep 28 '22 07:09

Binu