Добрый день, начал изучать спринг и я второй день бьюсь над запуском обычного теста, помогите ибо мои поиски пока результата не дали.
из стектраса вижу что еще ошибка с созданием бина в методе 'getEntityManagerFactoryBean' но не могу понять что мне туда добавить что бы ее решить!!!!! все примеры которые я находил и вних идентичная настройка, помогите решить этот вопрос, тыкните пальцем что мне нужно дописать!!
Класс JPAConfig:
@Configuration
@PropertySource("classpath:application-test.properties")
@EnableTransactionManagement
public class JpaConfigureTest {
@Autowired
private Environment environment;
@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactoryBean(){
LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean();
lcemfb.setJpaVendorAdapter(getJpaVendorAdapter());
lcemfb.setDataSource(getDataSource());
// lcemfb.setPersistenceUnitName("myJpaPersistenceUnit");
lcemfb.setPackagesToScan("com.model");
lcemfb.setJpaProperties(jpaProperties());
return lcemfb;
}
@Bean
public DataSource getDataSource(){
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName"));
driverManagerDataSource.setUrl(environment.getProperty("jdbc.url"));
driverManagerDataSource.setUsername(environment.getProperty("jdbc.username"));
driverManagerDataSource.setPassword(environment.getProperty("jdbc.password"));
return driverManagerDataSource;
}
@Bean
public JpaVendorAdapter getJpaVendorAdapter(){
JpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
return adapter;
}
@Bean
public PlatformTransactionManager txManager(){
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(
getEntityManagerFactoryBean().getObject());
return jpaTransactionManager;
}
private Properties jpaProperties(){
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getProperty("hibernate.dialect"));
properties.put("hibernate.hbm2ddl.auto", environment.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.show_sql", environment.getProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getProperty("hibernate.format_sql"));
return properties;
}
}
Class TestCofig
@Configuration
@ComponentScan(basePackages = {"com.dao.daoImpl",
"com.model",
"com.service.serviceImpl"})
public class TestConfig {
@Bean
public RoleImpl getRoleImpl(){
return new RoleImpl();
}
@Bean
public RoleServiceImpl getRoleServiceImpl(){
return new RoleServiceImpl();
}
}<code>
Сам класс теста
<code>@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@ContextConfiguration(classes = {JpaConfigureTest.class, TestConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class RoleDAOTest {
@Autowired
public RoleDAO roleDAO;
@Autowired
@Qualifier("roleService")
public RoleService roleService;
@Test
public void findAllRoles() {
}
@Test
public void findByRoleName() {
}
@Test
public void findById() {
}
@Test
public void save() {
Role role = new Role();
role.setNameRole("admin");
// Role role1 = new Role();
// role1.setNameRole("user");
//
// Role role2 = new Role();
// role2.setNameRole("none");
// User user = new User("Alex", "Login",
// "furriets@gmail.com", "111111111", LocalDate.now(), role);
// role.getUsers().add(user);
User user = new User();
user.setPassword("111111111");
user.setLogin("loginnnn");
user.setName("alexds");
user.setDate(LocalDate.now());
user.setEmail("furriets@gmail.com");
user.setRole(role);
role.getUsers().add(user);
// User user1 = new User("Alex1", "Login1",
// "furriets1@gmail.com", "222222222", LocalDate.now(), role1);
// role1.getUsers().add(user1);
//
// User user2 = new User("Alex2", "Login2",
// "furriets2@gmail.com", "333333333", LocalDate.now(), role2);
// role2.getUsers().add(user2);
roleService.saveRole(role);
}
}</code>
stackstace
<code>java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getEntityManagerFactoryBean' defined in test.JpaConfigureTest: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:246)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
... 24 more</code>