如何解决spring boot test中的空指针异常?

Posted

技术标签:

【中文标题】如何解决spring boot test中的空指针异常?【英文标题】:How to solve null pointer exception in spring boot test? 【发布时间】:2021-07-02 20:00:28 【问题描述】:

我有两个简单的消费者组件和一个消费者服务。然后我想为此服务编写单元测试。但总是出现空指针异常。

我尝试过:Autowire 构造、Autowire setter、将 mock 放入测试方法体、将 mock 放入 @Before 方法、添加/删除 @PrepareForTest(MyService.class) 等。它们都不起作用。

这是我的代码:

@Component
public class MyComponent1 implements Consumer<Employee>  

@Component
public class MyComponent2 implements Consumer<Employee>  

@Service
public class MyService implements Consumer<List<Employee>> 
    @Autowired
    private MyComponent1 myComponent1;
    @Autowired
    private MyComponent2 myComponent2;

    @Override
    public void accept(List<Employee> employeeList) 
        employeeList.forEach(myComponent1.andThen(myComponent2));
    


@SpringBootTest
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
//@PrepareForTest(MyService.class)
public class MyServiceTest 

    @MockBean
    private MyComponent1 mockedComponent1;

    @MockBean
    private MyComponent2 mockedComponent2;

    @Autowired
    private MyService myService;

    @Before
    public void init() 
        PowerMockito.doNothing().when(mockedComponent1).accept(any(Employee.class));
        PowerMockito.doNothing().when(mockedComponent2).accept(any(Employee.class));
    
    @Test
    public void myTest() 
        Employee employee1 = new Employee("abc", 43);
        List<Employee> employeeList = new LinkedList<>();
        employeeList.add(employee1);

        myService.accept(employeeList);

        verify(mockedComponent1, times(1)).accept(any(Employee.class));
        verify(mockedComponent2, times(1)).accept(any(Employee.class));
    

错误堆栈

java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:203)
    at java.lang.Iterable.forEach(Iterable.java:73)
    at com.wanghongliang.service.MyService.accept(MyService.java:31)
    at com.wanghongliang.service.MyServiceTest.myTest(MyServiceTest.java:54)
    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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    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)

【问题讨论】:

您的问题与this有关吗? 你为什么要为这两种类型模拟和提供 bean? 【参考方案1】:

问题是在你的被测方法中你调用了:

myComponent1.andThen(myComponent2)

由于myComponent1 是一个模拟并且您没有对andThen 方法调用进行存根,因此表达式的计算结果为null,您将其传递给forEach。

修复 1:

创建Consumer.class 的新模拟。我们就叫它combinedConsumer 存根myComponent1.andThen返回combinedConsumer

修复 2:

指示 Mockito 调用 andThen 的真实方法
Mockito.when(consumer1.andThen(consumer2)).thenCallRealMethod();

更一般的评论:

单次服务测试不需要@SpringBootTest。我会选择 MockitoExtension / Runner。由于当前需要创建整个 Spring 应用上下文,因此您的测试将不那么脆弱且速度更快。 doNothing 是 void 方法的默认操作(在您的情况下为 Consumer.accept)。你不需要存根。 Mockito 足以完成此测试(没有使用 PopwerMockito 特定的内容)

【讨论】:

以上是关于如何解决spring boot test中的空指针异常?的主要内容,如果未能解决你的问题,请参考以下文章

使用 Spring Boot 服务作为批处理作业中的依赖项的空指针异常

spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法

在 Spring Boot 2 中测试组件时的空指针

服务类Spring引导中的空指针异常[重复]

Spring 代理类和 Kotlin 中的空指针异常

Spring 代理类和 Kotlin 中的空指针异常