0

For some reason JUnit cannot wire Spring context correctly:

JUnit

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { ApplicationTestConfig.class, CachingConfig.class }, loader = AnnotationConfigContextLoader.class)
@Transactional(transactionManager = "hibernateTransactionManager")
public class QueryTest extends AbstractTransactionalJUnit4SpringContextTests {
//...}

ApplicationTestConfig

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.hibernate.query.performance.persistence" }, transactionManagerRef = "jpaTransactionManager")
@EnableJpaAuditing
@PropertySource({ "classpath:persistence-postgresql.properties" })
@ComponentScan(basePackages = { "com.hibernate.query.performance" },
        excludeFilters = @ComponentScan.Filter(value = {WebMvcConfigurerAdapter.class, WebConfig.class}, type = FilterType.ASSIGNABLE_TYPE))
public class ApplicationTestConfig extends ApplicationConfig {
}

ApplicationConfig

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories (basePackages = { "com.hibernate.query.performance.persistence" }, transactionManagerRef = "jpaTransactionManager")
@EnableJpaAuditing
@PropertySource({ "classpath:persistence-postgresql.properties" })
@ComponentScan({ "com.hibernate.query.performance" })
public class ApplicationConfig { //.. }

This one (WebConfig) should be excluded since ApplicationTestConfig has @ComponentScan.Filter:

WebMvcConfig

@Configuration
@ComponentScan("com.hibernate.query.performance.api")
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { //.. }

Setting @WebAppConfiguration in JUnit as suggested in this answer still produces:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: 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

Update

This does not work either:

@ComponentScan(basePackages = { "com.hibernate.query.performance" },
        excludeFilters = @ComponentScan.Filter(classes = {WebMvcConfigurer.class}, type = FilterType.ASSIGNABLE_TYPE))

and

@ComponentScan(basePackages = { "com.hibernate.query.performance" },
        excludeFilters = @ComponentScan.Filter(pattern = "WebConfig", type = FilterType.REGEX))

UPDATE2 Putting WebConfig inside a folder that is marked (in IDEA) as excluded hacked the problem....

Community
  • 1
  • 1
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185

0 Answers0