我正在与SpringBoot
和Hibernate ORM
合作。对于单元测试,我正在使用junit
。
仅运行测试用例时出现上述异常。
TestClass:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={ MainApp.class })
public class TestEmplService {
@Autowired EmployeeService eService;
@BeforeClass
public static void beforeTest() {
System.out.println("Starting test engine");
}
@AfterClass
public static void AfterTest() {
System.out.println("Terminating test engine");
}
@Test
public void getEmployeeById() {
Employee empObj = eService.findById(2);
assertNotNull(empObj);
System.out.println(empObj.toString());
}
}
实施类别:
@Repository
public class EmployeeServiceImpl extends QueryHelper implements EmployeeService{
@Autowired
private SessionFactory sessionFactory;
public EmployeeServiceImpl() {
super("/queries/employee.yml");
System.out.println(sessionFactory);
}
@Override
public Employee findById(int id) {
try {
System.out.println("Find by id "+sessionFactory);
Session session = sessionFactory.getCurrentSession();
System.out.println("Find by id "+session);
Employee employee = (Employee)session.get(Employee.class, id);
//emailService.sendMessage("vrushak.k@sonata-software.com", "Searched Data", employee.toString());
return employee;
}
catch(Throwable t) {
throw new RepositoryException(t);
}
}
}
Employee Service只是具有上述方法声明的接口。 当我仅运行TestCase时,出现上述异常。正常执行效果很好。
MainApp.java
@SpringBootApplication()
@EnableAutoConfiguration
public class MainApp {
public static void main(String... argsa) {
// TODO Auto-generated method stub
SpringApplication.run(MainApp.class, argsa);
}
@Bean()
public EmployeeService employeeServices(){
return new EmployeeServiceImpl();
}
}
主应用程序类包含employeeService
个注入,该注入在弹簧开始时立即加载。我想这个异常是在会话工厂的某个地方引起的。
HibernateConfig.java
@Configuration
public class HibernateConfig {
@Value("${db.driver}")
private String DB_DRIVER;
@Value("${db.password}")
private String DB_PASSWORD;
@Value("${db.url}")
private String DB_URL;
@Value("${db.username}")
private String DB_USERNAME;
@Value("${hibernate.dialect}")
private String HIBERNATE_DIALECT;
@Value("${hibernate.show_sql}")
private String HIBERNATE_SHOW_SQL;
@Value("${hibernate.hbm2ddl.auto}")
private String HIBERNATE_HBM2DDL_AUTO;
@Value("${entitymanager.packagesToScan}")
private String ENTITYMANAGER_PACKAGES_TO_SCAN;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
sessionFactory.setHibernateProperties(hibernateProperties);
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DB_DRIVER);
dataSource.setUrl(DB_URL);
dataSource.setUsername(DB_USERNAME);
dataSource.setPassword(DB_PASSWORD);
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
}
上面的配置负责加载会话工厂和数据源。
请让我知道我要去哪里错了?