一尘不染

JUnit测试(使用Spring MVC和Hibernate):IllegalArgumentException:未知实体

spring-mvc

我正在尝试做一些JUnit持久性测试。我正在使用Spring MVC和Hibernate。

我的TestConfig文件如下所示:

@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence", })
@Configuration
public class TestConfig {

    @Bean
    public DataSource dataSource() {
        final SimpleDriverDataSource ds = new SimpleDriverDataSource();
        ds.setDriverClass(JDBCDriver.class);
        ds.setUrl("jdbc:hsqldb:mem:paw");
        ds.setUsername("ha");
        ds.setPassword("");
        return ds;
    }

     @Bean
     public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
         final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
         factoryBean.setPackagesToScan("src.main.java.ar.edu.itba.paw.models");
         factoryBean.setDataSource(dataSource());
         final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
         factoryBean.setJpaVendorAdapter(vendorAdapter);
         final Properties properties = new Properties();
         properties.setProperty("hibernate.hbm2ddl.auto", "update");
         properties.setProperty("hibernate.search.default.directory_provider", "filesystem");
         properties.setProperty("hibernate.search.default.indexBase", "lucene/indexes");
         properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
         properties.setProperty("hibernate.show_sql", "true");
         properties.setProperty("format_sql", "true");
         factoryBean.setJpaProperties(properties);
         return factoryBean;
       }



     @Bean
     public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
          return new JpaTransactionManager(emf);
     }
}

这是我要运行的测试。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
@Transactional
public class UserHibernateDaoTest {

    private static final long USERID = 1;
    private static final long NONEXISTENTUSERID = -1;
    private static final String FIRSTNAME = "TestFirstName";
    private static final String LASTNAME = "TestLastName";
    private static final String EMAIL = "test1@mail.com";
    private static final String PASSWORD = "TestPassword";
    private static final String PHONENUMBER = "0000000";
    private static final String ROLE = "USER";

    @PersistenceContext
    private EntityManager em;

    @Autowired 
    private UserHibernateDao userHibernateDao;  
    private JdbcTemplate jdbcTemplate;

    @Before
    @Transactional
    public void setUp() {
        this.userHibernateDao = new UserHibernateDao();
            User u;
            u = new User();
            u.setUserid(123);
            u.setFirstName(FIRSTNAME);
            u.setLastName(LASTNAME);
            u.setEmail(EMAIL);
            u.setPassword(PASSWORD);
            u.setPhoneNumber(PHONENUMBER);
            u.setRole(ROLE);
            em.persist(u);

    }

    @Rollback
    @Test
    public void testCreate() {
    //  just trying to run this empty test
    }
}

问题是我什至不能运行那个简单的空测试,因为我遇到了几个异常,最后一个是:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ar.edu.itba.paw.persistence.UserHibernateDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

关于如何解决这个问题的任何想法?


阅读 332

收藏
2020-06-01

共1个答案

一尘不染

做测试时,您必须提供完整的spring配置,以实例化您的测试类。

在您的情况下,您可以为UserHibernateDao类属性设置自动装配,但是请注意,当前为Spring提供了一种连接此类的方法。当您直接在设置方法中实例化UserHibernateDao时。只需卸下自动线即可解决您的问题。

2020-06-01