springboot使用mock的一个问题。

在springboot+jpa+druid+mock项目中,进行单元测试

/**
 * @Description
 * @Author 夜羽 <[email protected]>
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2018/9/2
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class RecordControllerTest {

    @Autowired
    private MockMvc mvc;


    @Test
    public void findAllRecords() throws Exception {
        MvcResult result = mvc.perform(MockMvcRequestBuilders.get("/api/record/page").accept(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print()).andReturn();
        String contentbody = result.getResponse().getContentAsString();
        System.out.println(contentbody);
    }
}

会报一个空指针的错误:

java.lang.NullPointerException
    at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:94)
    at org.springframework.test.web.servlet.setup.PatternMappingFilterProxy.doFilter(PatternMappingFilterProxy.java:101)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:165)
    at com.sws.hdmshull.controller.RecordControllerTest.findAllRecords(RecordControllerTest.java:43)
    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)

解决方法有两个:

1: /** * web项目上下文 */

@Autowired

private WebApplicationContext webApplicationContext;

/** * 所有测试方法执行之前执行该方法 */

@Before public void before() {

//获取mockmvc对象实例

mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

}

手动生成mockmvc实例

第二种方式就是:

方法1,这样做是不加载其他的过滤器. 这样权限校验无法通过!

使用MockMvc测试时, WebStatFilter类其实是没有用的. 现在可用的解决方案是:

  • 加入 druid-spring-boot-starter 依赖
  • 在application-test.properties中增加配置spring.datasource.druid.web-stat-filter.enabled=false
  • 在测试类上增加@activeprofiles("test")

ps:比较推崇方法2,将环境分离

具体原因就是:

druid配置的filter未初始化

 

 

你可能感兴趣的:(springboot)