当前位置:   article > 正文

spring boot单元测试之druid NullPointException问题解决

druid nullpointexception

背景

最近单元测试使用了spock框架。

说实话,对于spock就是一小菜鸟。

groovy语法基本靠猜。

好不容跑起来了,却报了数据库连接从错误。

错误信息

  1. java.lang.NullPointerException
  2. at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:94)
  3. at org.springframework.test.web.servlet.setup.PatternMappingFilterProxy.doFilter(PatternMappingFilterProxy.java:101)
  4. at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)
  5. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
  6. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  7. at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)
  8. at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
  9. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  10. at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)
  11. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
  12. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  13. at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)
  14. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
  15. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  16. at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)
  17. at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)
  18. at com.dlxd.scau.web.ProductTypeControllerTest.testSave(ProductTypeControllerTest.java:34)
  19. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  20. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  21. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  22. at java.lang.reflect.Method.invoke(Method.java:498)
  23. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  24. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  25. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  26. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  27. at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
  28. at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
  29. at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
  30. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  31. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
  32. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
  33. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  34. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  35. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  36. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  37. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  38. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  39. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  40. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  41. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
  42. at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
  43. at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  44. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
  45. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
  46. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)

看的头皮发麻,简单来讲,就是没有连上数据库。

根据网络上有人分析,具体原因是因为druid配置的filter未初始化

问题解决方案

百度了两个解决方案(对我没用)

方法1:

单元测试中添加如下代码

  1. /**
  2. * web项目上下文
  3. */
  4. @Autowired
  5. private WebApplicationContext webApplicationContext;
  6. /**
  7. * 所有测试方法执行之前执行该方法
  8. */
  9. @Before
  10. public void before() {
  11. //获取mockmvc对象实例
  12. mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

方法2:

使用MockMvc测试时, WebStatFilter类其实是没有用的.

现在可用的解决方案是:

加入 druid-spring-boot-starter 依赖

在application-test.properties中增加配置

spring.datasource.druid.web-stat-filter.enabled=false

在测试类上增加@activeprofiles("test") 

--------------------------------------------贼溜溜的分界线-------------------------------------------------------

我找到的有效解决方案

有效解决方案

在@SpringBootTest注解上添加webEnvironment的注解值

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

简单看了下代码。发现SpringBootTest.WebEnvironment默认值是MOCK。

MOCK的话好像不能初始化druid。

为什么这个webEnvironment=WebEnvironment.MOCK参数可以控制Filter的初始化过程?

建议你可以看下这篇文章,说的应该比较详细

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/274460
推荐阅读
相关标签
  

闽ICP备14008679号