Spring-boot test

时间:2017-05-23 15:38:35

标签: java eclipse spring-boot junit

我是编程世界的新手,所以我说的看起来很傻。

我正在尝试在Eclipse下运行Spring启动测试作为JUnit,但我无法弄清楚如何使用spring-boot注释...我已经阅读了几个指南并浏览了这个网站,但没有找到解决我问题的任何事情。

我正在尝试运行下面的JUnit测试类:

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

将此作为JUnit测试运行会给出以下异常:

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 

CDao类的EntityManager参数有@PersistenceContext注释,我认为这意味着它是由Hibernate自动生成的,但显然它不是......我怎样才能使用java代码实现EntityManager?我没有任何.xml或.properties文件...

FYI这里是测试调用的类:

业务层:

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

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

服务层:

@Service
public class CService {
    @Autowired
    CDao cDao;

Dao Layer

@Repository
@Transactional(rollbackFor = {TechnicalException.class})
public class CDao {

    @PersistenceContext
    EntityManager entityManager;

我尝试在Web服务中使用Business层上的@autowire注释测试方法,如果工作正常,但是我无法在JUnit测试中实例化它。我尝试了几种运行此测试的方法,我不确定这是正确的方法,所以我愿意接受任何建议。

提前致谢。

1 个答案:

答案 0 :(得分:7)

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