Skip to content

NPE occurs when use @Reference in junit in spring boot application #3429

Closed

Description

@meixinbin

运行环境:
jdk:1.8
dubbo:2.6.5
spring boot 2.0.4

问题描述:
在spring boot项目中使用Junit 测试dubbo服务,@reference 注解报空指针异常。而我用@Autowired 注解是没有问题的,并且用dubbo 2.5.2 及dubbo 2.8.4 都没有问题。

default

下图是我debug到dubbo的代码看到的,其中的服务Bean并没有被注入

default

下面是我用@Autowired 测试的结果,没有任何问题

default

default

这个问题只有在单元测试的时候出现,服务在其他系统里面是可以正常调用的

Activity

  1. meixinbin commented on Feb 3, 2019

    @meixinbin
    Author

    我调试了一下,就是因为ReferenceBeanInvocationHandler 类的init方法没有调用导致bean属性为null,
    我把这个类做了如下修改:
    default

    在ReferenceBeanInvocationHandler 的构造方法里面调用init方法,解决了这个问题,但是不清楚这样做会不会有其它的问题。
    下面是修改后的Junit测试结果:
    default

  2. tswstarplanet commented on Feb 3, 2019

    @tswstarplanet
    Contributor

    Would you please provide a demo project that can reproduce the problem?

  3. meixinbin commented on Feb 11, 2019

    @meixinbin
    Author

    我已经把相关demo上传到github
    https://github.com/meixinbin/dubbo-test

    把项目里面的com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor 类删除即可重现bug

  4. meixinbin commented on Feb 12, 2019

    @meixinbin
    Author

    @mercyblitz
    yes I do

  5. meixinbin commented on Feb 13, 2019

    @meixinbin
    Author

    @tswstarplanet
    @mercyblitz
    Has the problem been solved?

  6. feixz commented on Feb 21, 2019

    @feixz
    Contributor

    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/

    @RunWith(SpringRunner::class)
    @SpringBootTest
    class UserServiceImplTest {
    
        // Place definition above class declaration to make field static
        private val _logger = KotlinLogging.logger {}
    
        @Reference(check = false, timeout = 10000)
        private lateinit var userService: UserService
    
        @Test
        fun getByUsernameOrPhoneNumOrEmail() {
            userService.getByUsernameOrPhoneNumOrEmail("test")
        }
    }
    
    
    java.lang.NullPointerException
    	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.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceBeanInvocationHandler.invoke(ReferenceAnnotationBeanPostProcessor.java:158)
    	at com.sun.proxy.$Proxy147.getByUsernameOrPhoneNumOrEmail(Unknown Source)
    	at com.jidian.cosalon.upms.service.impl.UserServiceImplTest.getByUsernameOrPhoneNumOrEmail(UserServiceImplTest.kt:26)
    	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.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)
    
    
  7. added this to the 2.6.6 milestone on Feb 22, 2019
  8. self-assigned this
    on Feb 22, 2019
  9. added a commit that references this issue on Feb 22, 2019
  10. added 2 commits that reference this issue on Mar 14, 2019
  11. modified the milestones: 2.6.6, 2.7.1 on Mar 15, 2019
  12. changed the title [-]spring boot项目中使用Junit 测试dubbo服务,@Reference 注解报空指针异常[/-] [+]NPE occurs when use @Reference in junit in spring boot application[/+] on Mar 15, 2019
  13. added a commit that references this issue on Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions