我为我的Spring-Boot 1.3应用程序运行了一组集成测试。但我必须添加以下内容才能使我的最大会话数正常工作:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter implements ServletContextAware {
...
@Override
public void setServletContext(ServletContext servletContext) {
servletContext.getSessionCookieConfig().setHttpOnly(true);
// causes an ApplicationEvent to be published to the Spring ApplicationContext every time a HttpSession commences or terminates
servletContext.addListener(new HttpSessionEventPublisher());
}
...
}现在,当我运行我的测试时,我得到以下结果:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig' defined in file [/Users/davidclark/projects/edmtotal/build/classes/main/com/edelweissco/dental/configuration/WebSecurityConfig.class]: Initialization of bean failed; nested exception is java.lang.UnsupportedOperationException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
...
Caused by: java.lang.UnsupportedOperationException
at org.springframework.mock.web.MockServletContext.addListener(MockServletContext.java:675)
at com.edelweissco.dental.configuration.WebSecurityConfig.setServletContext(WebSecurityConfig.java:123)
...下面是一个示例测试类(但它们都属于同一个异常):
@Transactional
public class ConfigurationSettingsTest extends BaseSpecification {
@Autowired
private ConfigurationSettings configurationSettings;
@Autowired
ConfigurableApplicationContext context
...
}其中,BaseSpecification是:
@ContextConfiguration(classes = MyApp, loader = SpringApplicationContextLoader)
@WebAppConfiguration
public class BaseSpecification extends Specification {
@Value('${local.server.port}')
private int serverPort;
def setup() {
RestAssured.port = serverPort;
}
}现在,当我运行集成测试时,似乎在这里应用了一个MockServlet,但它不支持。此功能。在调试时,我看到一个SpringBootMockServletContext正试图在setServletContext中设置,这就是异常所在。
发布于 2015-11-06 06:21:11
我会张贴我的答案,以防其他人遇到这个问题。问题出在我的BaseSpecification上。我在其中添加了@WebAppConfiguration和@IntegrationTest,并从各个集成测试中删除了@IntegrationTest。显然,这实际上将创建ServletContext的应有方式。
@ContextConfiguration(classes = MyApp, loader = SpringApplicationContextLoader)
@WebAppConfiguration
@IntegrationTest
public class BaseSpecification extends Specification {https://stackoverflow.com/questions/33531506
复制相似问题