Guest User

Untitled

a guest
Oct 11th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. @Configuration
  2. @ComponentScan(basePackages = {"ru.example"})
  3. @PropertySource("classpath:/config/app.properties")
  4. public class RootConfig {
  5.  
  6. @Bean
  7. public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
  8. return new PropertySourcesPlaceholderConfigurer();
  9. }
  10.  
  11. }
  12.  
  13. @Configuration
  14. @EnableTransactionManagement
  15. @EnableJpaRepositories("ru.example.repository")
  16. @EntityScan("ru.example.domain")
  17. public class TransactionManagerConfig {
  18.  
  19. @Bean
  20. public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws PropertyVetoException {
  21. LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  22. entityManagerFactoryBean.setDataSource(dataSource());
  23. HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  24. vendorAdapter.setGenerateDdl(true);
  25. entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
  26. entityManagerFactoryBean.setPackagesToScan("ru.example.domain");
  27. return entityManagerFactoryBean;
  28. }
  29.  
  30. @Bean
  31. public JpaTransactionManager transactionManager() throws PropertyVetoException {
  32. JpaTransactionManager transactionManager = new JpaTransactionManager();
  33. transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
  34. return transactionManager;
  35. }
  36.  
  37. private DataSource dataSource() throws PropertyVetoException {
  38. ComboPooledDataSource dataSource = new ComboPooledDataSource();
  39. dataSource.setDriverClass("com.mysql.jdbc.Driver");
  40. dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/example");
  41. dataSource.setUser("root");
  42. dataSource.setPassword("root");
  43. dataSource.setMaxPoolSize(20);
  44. dataSource.setMinPoolSize(3);
  45. dataSource.setMaxStatements(100);
  46. dataSource.setPreferredTestQuery("SELECT 1");
  47. dataSource.setTestConnectionOnCheckout(true);
  48. return dataSource;
  49. }
  50.  
  51. }
  52.  
  53. @Configuration
  54. @EnableWebMvc
  55. @ComponentScan({"ru.example"})
  56. public class WebConfig implements WebMvcConfigurer {
  57.  
  58. @Bean
  59. @SuppressWarnings("unused")
  60. public ViewResolver viewResolver() {
  61.  
  62. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  63. resolver.setPrefix("/WEB-INF/views/");
  64. resolver.setSuffix(".jsp");
  65. resolver.setExposeContextBeansAsAttributes(true);
  66. return resolver;
  67. }
  68.  
  69. @Override
  70. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  71.  
  72. registry
  73. .addResourceHandler("/**")
  74. .addResourceLocations("/");
  75. }
  76. }
  77.  
  78. testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: JUNIT_VERSION
  79. testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: JUNIT_VERSION
  80. testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.3.1'
  81. testCompile group: 'org.mockito', name: 'mockito-core', version: '2.22.0'
  82. testCompile group: 'org.springframework', name: 'spring-test', version: SPRING_VERSION
  83. testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
  84. testCompile group: 'com.jayway.jsonpath', name: 'json-path-assert', version: '2.4.0'
  85. compile group: 'org.springframework', name: 'spring-webmvc', version: SPRING_VERSION
  86. compile group: 'org.springframework', name: 'spring-web', version: SPRING_VERSION
  87. compile group: 'org.springframework', name: 'spring-jdbc', version: SPRING_VERSION
  88. compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.5.RELEASE'
  89.  
  90. SPRING_VERSION = '5.1.0.RELEASE'
  91. JUNIT_VERSION = '5.3.1'
  92.  
  93. @ExtendWith(SpringExtension.class)
  94. @ContextConfiguration(classes = {
  95. RootConfig.class,
  96. TransactionManagerConfig.class,
  97. WebConfig.class
  98. })
  99. class OrderServiceImplTest {
  100.  
  101. @Test
  102. void save() {
  103. }
  104. }
  105.  
  106. Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
  107. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
  108. at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:620)
  109. ... 94 common frames omitted
  110. Caused by: java.lang.IllegalStateException: No ServletContext set
  111. at org.springframework.util.Assert.state(Assert.java:73)
  112. at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:486)
  113. at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$b4bf2d2e.CGLIB$resourceHandlerMapping$34(<generated>)
  114. at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$b4bf2d2e$$FastClassBySpringCGLIB$$d653d6e3.invoke(<generated>)
  115. at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
  116. at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
  117. at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$b4bf2d2e.resourceHandlerMapping(<generated>)
  118. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  119. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  120. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  121. at java.lang.reflect.Method.invoke(Method.java:498)
  122. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
  123. ... 95 common frames omitted
Add Comment
Please, Sign In to add comment