2015-11-04 54 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 {