Ich möchte Test AnnManagerTest.java erstellen, aber einen Fehler nach der Ausführung erhalten: Fehler beim Laden von ApplicationContext.Führen Sie Test.class, aber ich kann nicht geladen werden ApplicationContext
Dies ist mein Code:
1.SpringConfiguration.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.*"})
@EnableTransactionManagement
public class SpringConfiguration extends WebMvcConfigurerAdapter {
@Bean(name = "multipartResolver")
public StandardServletMultipartResolver resolver() {
return new StandardServletMultipartResolver();
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(InternalResourceView.class);
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor());
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPersistenceUnitName("MyKMPersistenceUnit");
em.setPackagesToScan(new String[]{"com.model"});
em.setJpaDialect(new HibernateJpaDialect());
em.setJpaPropertyMap(jpaProperties());
em.setJpaVendorAdapter(jpaVendorAdapter());
em.setPersistenceXmlLocation("classpath:/persistenceXXX.xml");
return em;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.HSQL);
jpaVendorAdapter.setShowSql(true);
jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.HSQLDialect");
return jpaVendorAdapter;
}
@Bean
public Map<String, Object> jpaProperties() {
Map<String, Object> props = new HashMap<>();
props.put("hibernate.dialect", DerbyTenSevenDialect.class.getName());
props.put("hibernate.hbm2ddl.auto", "none");
props.put("hibernate.show_sql", "true");
props.put("hibernate.format_sql", "true");
props.put("hibernate.connection.charSet", "UTF-8");
return props;
}
@Bean
public DataSource dataSource() {
DataSource dataSource = new DataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/KM");
dataSource.setUsername("username");
dataSource.setPassword("passwd");
dataSource.setInitialSize(10);
dataSource.setMaxActive(100);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() throws Exception {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
jpaTransactionManager.setDataSource(dataSource());
jpaTransactionManager.setJpaDialect(new HibernateJpaDialect());
return jpaTransactionManager;
}
@Bean
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public SpringContextUtil springContextUtil() {
return new SpringContextUtil();
}
}
2.SpringContextUtil.java
public class SpringContextUtil implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
}
3.AnnManagerTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class}, loader = AnnotationConfigContextLoader.class)
public class AnnManagerTest extends AbstractTransactionalJUnit4SpringContextTests {
@Test
public void testDoModify() throws Exception {
ApplicationContext ctx = SpringContextUtil.getApplicationContext();
AnnouncementHeaderService service = (AnnouncementHeaderService) ctx.getBean("announcementHeaderService");
AnnouncementHeader announcementHeader = new AnnouncementHeader();
announcementHeader.setAnnID(40);
announcementHeader = service.findByOne(announcementHeader);
}
}
Dies ist meine Ausnahme:
2016-07-27 23:15:03 WARN GenericApplicationContext:546 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
2016-07-27 23:15:03 INFO LocalContainerEntityManagerFactoryBean:481 - Closing JPA EntityManagerFactory for persistence unit 'MyKMPersistenceUnit'
2016-07-27 23:15:03 ERROR TestContextManager:231 - Caught exception while allowing TestExecutionListener [org.springframewor[email protected]4f6ee6e4] to prepare test instance [[email protected]]
java.lang.IllegalStateException: Failed to load ApplicationContext
Wer kann mir sagen, was? Vielen Dank!!