简体   繁体   English

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

[英]JUnit Testing (with Spring MVC and Hibernate): IllegalArgumentException: Unknown entity

I'm tryng to do some JUnit persistance tests.我正在尝试做一些 JUnit 持久性测试。 I'm using Spring MVC and Hibernate.我正在使用 Spring MVC 和 Hibernate。

I have my TestConfig file looking like this:我的 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);
     }
}

And here's the test I'm trying to run.这是我正在尝试运行的测试。

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

The thing is I'm not even able to run that simple empty test because I get a couple of exceptions, the last one being:问题是我什至无法运行那个简单的空测试,因为我有几个例外,最后一个是:

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

Any ideas on how to solve this?关于如何解决这个问题的任何想法?

When doing test you must give a complete spring config to be able to instantiate your test class.进行测试时,您必须提供完整的 spring 配置才能实例化您的测试类。

In your case you have an autowire for the UserHibernateDao class attribute, but noting currently give a way for spring to wire this class.在您的情况下,您有一个 UserHibernateDao 类属性的自动装配,但请注意当前为 spring 提供了一种连接此类的方法。 As you are directly instantiating the UserHibernateDao inside your setup method.当您在设置方法中直接实例化 UserHibernateDao 时。 Just removing the autowire should fix your issue.只需移除 autowire 即可解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM