JUnit测试(使用Spring MVC和Hibernate):IllegalArgumentException:未知实体
我正在尝试进行一些JUnit持久性测试.我正在使用Spring MVC和Hibernate.
I'm tryng to do some JUnit persistance tests. I'm using Spring MVC and Hibernate.
我的TestConfig文件如下所示:
I have my TestConfig file looking like this:
@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
}
}
问题是我什至无法运行那个简单的空测试,因为我遇到了几个异常,最后一个是:
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?
进行测试时,必须提供完整的spring配置才能实例化测试类.
When doing test you must give a complete spring config to be able to instantiate your test class.
在您的情况下,您可以为UserHibernateDao类属性设置自动装配,但请注意,当前为spring提供了一种连接此类的方法.当您直接在设置方法中实例化UserHibernateDao时.只需卸下自动线即可解决您的问题.
In your case you have an autowire for the UserHibernateDao class attribute, but noting currently give a way for spring to wire this class. As you are directly instantiating the UserHibernateDao inside your setup method. Just removing the autowire should fix your issue.