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

Posted 沛沛老爹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot单元测试之druid NullPointException问题解决相关的知识,希望对你有一定的参考价值。

背景

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

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

groovy语法基本靠猜。

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

错误信息

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:127)

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:127)

at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)

at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)

at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)

at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)

at com.dlxd.scau.web.ProductTypeControllerTest.testSave(ProductTypeControllerTest.java:34)

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.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)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)

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

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

问题解决方案

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

方法1:

单元测试中添加如下代码

/**

* web项目上下文

*/

@Autowired

private WebApplicationContext webApplicationContext;

/**

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

*/

@Before

public void before() 

//获取mockmvc对象实例

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的初始化过程?

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

 

以上是关于spring boot单元测试之druid NullPointException问题解决的主要内容,如果未能解决你的问题,请参考以下文章

单元测试中的 Spring Boot @Autowired 返回 NullPointerException

06.深入浅出 Spring Boot - 数据访问之Druid

Spring Boot 进阶之Web进阶 学习 - 单元测试

Spring Boot单元测试入门实战之关于JUnit

Spring Boot 集成 Druid 批量插入数据和效率监控配置

Spring Boot 集成 Druid 批量插入数据和效率监控配置