Я пытаюсь сделать несколько тестов на устойчивость 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)}
Есть идеи, как это решить?