2015-11-04 59 views
1

我有一套集成测试运行我的Spring-Boot 1.3应用程序。但是,我不得不添加下面让我的最大会话工作:弹簧测试失败mockServletContext不受支持操作

@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中设置,这就是例外情况。

回答

1

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

@ContextConfiguration(classes = MyApp, loader = SpringApplicationContextLoader) 
@WebAppConfiguration 
@IntegrationTest 
public class BaseSpecification extends Specification {