You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NPE occurs when use @Reference in junit in spring boot application #3429
@RunWith(SpringRunner::class)
@SpringBootTest
classUserServiceImplTest {
// Place definition above class declaration to make field staticprivateval_logger=KotlinLogging.logger {}
@Reference(check =false, timeout =10000)
privatelateinitvar userService:UserService
@Test
fungetByUsernameOrPhoneNumOrEmail() {
userService.getByUsernameOrPhoneNumOrEmail("test")
}
}
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
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.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceBeanInvocationHandler.invoke(ReferenceAnnotationBeanPostProcessor.java:158)
at com.sun.proxy.$Proxy147.getByUsernameOrPhoneNumOrEmail(UnknownSource)
at com.jidian.cosalon.upms.service.impl.UserServiceImplTest.getByUsernameOrPhoneNumOrEmail(UserServiceImplTest.kt:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
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.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
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:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
changed the title [-]spring boot项目中使用Junit 测试dubbo服务,@Reference 注解报空指针异常[/-][+]NPE occurs when use @Reference in junit in spring boot application[/+]on Mar 15, 2019
Activity
meixinbin commentedon Feb 3, 2019
我调试了一下,就是因为ReferenceBeanInvocationHandler 类的init方法没有调用导致bean属性为null,

我把这个类做了如下修改:
在ReferenceBeanInvocationHandler 的构造方法里面调用init方法,解决了这个问题,但是不清楚这样做会不会有其它的问题。

下面是修改后的Junit测试结果:
tswstarplanet commentedon Feb 3, 2019
Would you please provide a demo project that can reproduce the problem?
meixinbin commentedon Feb 11, 2019
我已经把相关demo上传到github
https://github.com/meixinbin/dubbo-test
把项目里面的com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor 类删除即可重现bug
mercyblitz commentedon Feb 12, 2019
Did you have used https://github.com/apache/incubator-dubbo-spring-boot-project ?
meixinbin commentedon Feb 12, 2019
@mercyblitz
yes I do
meixinbin commentedon Feb 13, 2019
@tswstarplanet
@mercyblitz
Has the problem been solved?
feixz commentedon Feb 21, 2019
I also encountered the same problem, unable to inject in junit
:: Dubbo Spring Boot (v2.7.0) : https://github.com/apache/incubator-dubbo-spring-boot-project
:: Dubbo (v2.7.0) : https://github.com/apache/incubator-dubbo
:: Discuss group : [email protected]
[DUBBO] Unsubscribe: provider://192.168.200.232:8000/
Polish apache#3429 : Fix the NPE
[Enhancement] inline the source code of spring-context-support (#3225)
Polish apache#3429 : Fix The NPE issue
Polish /apache#3429 : Fix The NPE issue
Enhancement and Bugfix in 2.7.1 ( Part 1 ) (#3670)
[-]spring boot项目中使用Junit 测试dubbo服务,@Reference 注解报空指针异常[/-][+]NPE occurs when use @Reference in junit in spring boot application[/+]fix default setting (#4955)