构造函数注入中的Nullpointer异常,可与字段注入Mockito一起使用

时间:2018-07-20 06:26:26

标签: java spring mockito lombok

我有一个抽象类,该类具有自动装配的依赖项:

public abstract class ClassB {
    @Autowired
    private ClassC classC;

    public String getValue() {
        classC.getSomeMethod();
    }
}

我有一个扩展此抽象类的类:

@Component
public class ClassA extends ClassB {
    @Autowired
    private ClassD classD;

    public String getClassAMethod() {
        String value = getValue();
        String dReturn = classD.getD();
        return value + dReturn;
    }
}

现在在进行单元测试时,我可以做:

public class ClassATest {
    @Mock
    private ClassC classC;

    @Mock
    private ClassD classD;

    @InjectMocks
    private ClassA classA;

    @Test
    public void testSomething() {
        when(classC.getSometMethod()).thenReturn("classC");
        when(classD.getD()).thenReturn("classD");
        assertEquals(classA.getClassAMethod(), "classCclassD");
    }

}

这很好,但是,如果我对ClassA使用构造函数注入,则会得到classC的Null指针异常。

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClassA extends ClassB {
    private final ClassD classD;

    public String getClassAMethod() {
       String value = getValue();
       String dReturn = classD.getD();
       return value + dReturn;
    }
}

在第二种情况下,我什至尝试用普通的构造函数调用替换InjectMocks,但不会模拟ClassC对象。

堆栈跟踪: 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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 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.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.mockito.internal.runners.DefaultInternalRunner$1.run(Unknown Source) at org.mockito.internal.runners.DefaultInternalRunner.run(Unknown Source) at org.mockito.internal.runners.StrictRunner.run(Unknown Source) at org.mockito.junit.MockitoJUnitRunner.run(Unknown Source) 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)

1 个答案:

答案 0 :(得分:0)

Lombok的@AllArgsConstructor@RequiredArgsConstructor仅覆盖在类本身中声明的字段。不考虑来自超类的字段,因为lombok无法访问这些类,因为在编译过程中lombok运行时尚未解析它们。

作为您的结果,为ClassA生成的构造函数仅以ClassD classD作为参数,而没有ClassC classC作为参数。 (您可以在IDE的类大纲视图中或通过分拆代码来看到这一点。)因此,classC仍未初始化,从而导致NPE。

因此,不幸的是,龙目岛无法为您提供帮助。您必须手动编写构造函数。