首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mockServletContext unsupportedOperation上的spring测试失败

mockServletContext unsupportedOperation上的spring测试失败
EN

Stack Overflow用户
提问于 2015-11-05 04:36:08
回答 1查看 1.2K关注 0票数 2

我为我的Spring-Boot 1.3应用程序运行了一组集成测试。但我必须添加以下内容才能使我的最大会话数正常工作:

代码语言:javascript
复制
@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());
    }

...
}

现在,当我运行我的测试时,我得到以下结果:

代码语言:javascript
复制
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)
...

下面是一个示例测试类(但它们都属于同一个异常):

代码语言:javascript
复制
@Transactional
public class ConfigurationSettingsTest extends BaseSpecification {

    @Autowired
    private ConfigurationSettings configurationSettings;

    @Autowired
    ConfigurableApplicationContext context

...
}

其中,BaseSpecification是:

代码语言:javascript
复制
@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中设置,这就是异常所在。

EN

回答 1

Stack Overflow用户

发布于 2015-11-06 06:21:11

我会张贴我的答案,以防其他人遇到这个问题。问题出在我的BaseSpecification上。我在其中添加了@WebAppConfiguration和@IntegrationTest,并从各个集成测试中删除了@IntegrationTest。显然,这实际上将创建ServletContext的应有方式。

代码语言:javascript
复制
@ContextConfiguration(classes = MyApp, loader = SpringApplicationContextLoader)
@WebAppConfiguration
@IntegrationTest
public class BaseSpecification extends Specification {
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33531506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档