EntityManagerFactory Bean in Spring-boot test

I am new to the programming world, so what I am saying may seem silly.

I'm trying to run a spring-boot test as JUnit under Eclipse, but I just can't figure out how to use spring-boot annotations ... I've read several tutorials and looked at this site, but couldn't find anything that solved 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 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 @PersistenceContext annotation, I thought this means it is automatically generated by Hibernate, but apparently it is not ... How can I create an EntityManager using only Java code? I don't have any .xml or .properties files ...

FYI are classes called test:

Business level:

@Component("cBusiness")
public class CBusiness {
    @Autowired
    CService cService;

public long calcul(Object object) throws TechnicalException {
//Code (calls a method from CService class)
}

      

Service level:

@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 the web service using only the @autowire annotation at the Business level, and if it works fine, I just can't install it in the JUnit tests. I've tried several ways to run this test, and I'm not sure if this is the correct way to do it, so I'm open to any suggestion.

Thanks in advance.

+3


source to share


1 answer


@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;
   }

      



+3


source







All Articles