spring开机测试空指针异常如何解决?
How to solve null pointer exception in spring boot test?
我有两个简单的消费者组件和一个消费者服务。然后我想为这个服务写单元测试。但是总是空指针异常。
我尝试过:Autowire构造,Autowire setter,将mock放入测试方法体,将mock放入@Before方法,Add/Remove @PrepareForTest({MyService.class}),等等 None 他们正在工作。
这是我的代码:
@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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)
问题是在你的被测方法中你调用了:
myComponent1.andThen(myComponent2)
由于 myComponent1
是一个 mock 并且您没有存根 andThen
方法调用,因此表达式的计算结果为 null,您将其传递给 forEach。
修复 1:
- 创建
Consumer.class
的新模拟。我们称它为 combinedConsumer
- 存根
myComponent1.andThen
到 return combinedConsumer
修复 2:
- 指示 Mockito 为
andThen
调用真正的方法
Mockito.when(consumer1.andThen(consumer2)).thenCallRealMethod();
更一般的评论:
- 单个服务测试不需要
@SpringBootTest
。我会选择 MockitoExtension / Runner。您的测试将不那么脆弱且速度更快,因为目前需要创建整个 spring 应用上下文。
doNothing
是 void 方法的默认操作(在您的情况下为 Consumer.accept
)。你不需要存根。
- Mockito 足以进行此测试(未使用任何 PopwerMockito 特定)
我有两个简单的消费者组件和一个消费者服务。然后我想为这个服务写单元测试。但是总是空指针异常。
我尝试过:Autowire构造,Autowire setter,将mock放入测试方法体,将mock放入@Before方法,Add/Remove @PrepareForTest({MyService.class}),等等 None 他们正在工作。
这是我的代码:
@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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)
问题是在你的被测方法中你调用了:
myComponent1.andThen(myComponent2)
由于 myComponent1
是一个 mock 并且您没有存根 andThen
方法调用,因此表达式的计算结果为 null,您将其传递给 forEach。
修复 1:
- 创建
Consumer.class
的新模拟。我们称它为combinedConsumer
- 存根
myComponent1.andThen
到 returncombinedConsumer
修复 2:
- 指示 Mockito 为
andThen
调用真正的方法
Mockito.when(consumer1.andThen(consumer2)).thenCallRealMethod();
更一般的评论:
- 单个服务测试不需要
@SpringBootTest
。我会选择 MockitoExtension / Runner。您的测试将不那么脆弱且速度更快,因为目前需要创建整个 spring 应用上下文。 doNothing
是 void 方法的默认操作(在您的情况下为Consumer.accept
)。你不需要存根。- Mockito 足以进行此测试(未使用任何 PopwerMockito 特定)