Spring MockMVC FilterChainProxy.getFilters()空指针异常

时间:2018-04-20 15:11:32

标签: spring filter chain mockmvc

我正在尝试使用MockMVC测试我们的OAuth资源服务器端点,但是我在配置安全过滤器链代理时遇到了问题。

下面是我的Test Class的代码,其中定义了调用我的ItemController中列出的端点的单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes ={FilterChainProxy.class})
public class ItemCTest {

@InjectMocks
private ItemController itemController;


@Autowired
private FilterChainProxy springSecurityFilterChain;


private MockMvc  mockMvc;

@Before
public void setup() {
    System.out.println(springSecurityFilterChain);
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.standaloneSetup(itemController).apply(springSecurity(springSecurityFilterChain)).build();
}

我的配置目录中也有以下类,这些类可能与实际问题有关,也可能与实际问题无关。 :

WebSecurityAppInitializer

public class WebSecurityAppInitializer extends AbstractSecurityWebApplicationInitializer {
}

WebSecurityConfig

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {

}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

}
}

当我尝试运行我的测试类中定义的样本测试时,这是一个可以吐出的日志消息:

java.lang.NullPointerException
at org.springframework.security.web.FilterChainProxy.getFilters(FilterChainProxy.java:224)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:197)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)
at com.vertexinc.ventures.resourceserver.controller.ItemCTest.test(ItemCTest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)

请注意,当我启动服务器并通过Postman ping端点时一切正常,所以我知道配置没有问题。我不知道为什么该配置没有应用于Test类,可以这么说。

1 个答案:

答案 0 :(得分:0)

我认为这与springSecurityFilterChain bean not found using MockMVC and @ComponentScan有关。

我遇到了这个问题,然后我将我的安全配置添加到@ContextConfiguration。

所以,上面的代码会有这个注释。

@ContextConfiguration(classes = {
    FilterChainProxy.class,
    WebSecurityConfig.class
})