【问题标题】:Null after @InjectMocks@InjectMocks 后为空
【发布时间】:2015-01-19 19:48:22
【问题描述】:

在使用 JUnit 进行单元测试时,我在传递依赖项时遇到了一些麻烦。

考虑这些代码:

这是我要测试的类的依赖注入,我们称之为控制器。

@Inject private FastPowering fastPowering;  

这是单元测试:

@RunWith(MockitoJUnitRunner.class)
public class ControllerTest {

@Mock
FastPowering fastPower;

@InjectMocks
Controller controller;

@Test
public void test() {
    assertEquals(
            (controller.computeAnswer(new BigDecimal(2), 2)).longValue(),
            (long) Math.pow(2, 2));
    }
}

fastPower 似乎为空,请说明如何解决此问题。 空指针异常,因为在 .computeAnswer 方法中调用了@injected 字段(fastPower)

编辑:

解决了我应该了解@Mock 和@Spy 之间的区别...

由于有很多 cmets,我正在为解决方案添加更多上下文

不同之处在于,在 mock 中,您正在创建一个完整的 mock 或假对象,而在 spy 中,有真实的对象,您只是监视或存根它的特定方法。当然,在 spy 对象中,由于它是一个真实的方法,当你不存根该方法时,它会调用真实的方法行为。

如果 fastPower 被注释为 @Mock 它的方法是虚拟的,但是 controller.computeAnswer 依赖于它们来计算。必须提供行为。

如果在没有存根的情况下使用 spy,则正在执行 fastPower 的真正实现,最终返回所需的值。

另一种选择是使用真正的 FastPowering 实例

https://github.com/mockito/mockito/wiki/Using-Spies-(and-Fakes) https://github.com/mockito/mockito/wiki/Mocking-Object-Creation

还有一些stackoverflow线程概述了区别Mocking vs. Spying in mocking frameworks

简短回答:将 @Mock 替换为 @Spy 应该可以正常工作

【问题讨论】:

  • 你的错误是什么?另外,computeAnswer 中的代码是什么?
  • 空指针异常,computeAnswer 使用 fastPower 中的方法计算 a^power 的值,这就是为什么它在调用时返回空指针失败
  • 方法是静态的还是最终的?
  • @rpet 你找到解决办法了吗?
  • “解决了我应该阅读@Mock 和@Spy 之间的区别...” 是否可以发布并接受您的解决方案作为答案?这可能会帮助其他有相同问题的用户并关闭此线程。

标签: java null mocking mockito inject


【解决方案1】:

使用MockitoAnnotations.initMocks 来启动@Mock 和@InjectMocks 对象。您的测试将类似于:

@Mock
FastPowering fastPower;

@InjectMocks
Controller controller;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
}

@Test
public void test() {
    ....
}

【讨论】:

  • 同样的问题,@InjectMocks 和 MockitoAnotations.initMocks(this) 之后的 NPE
  • @VB_ 你能解决这个问题吗?如果是,怎么做?
  • 解决方法无效
  • 我能够使用 BeforeEach 而不是 Before
【解决方案2】:

还有 2 件事需要检查:

1. Mocking the behaviours of fastPower. What should this mocked object return, when it methods are called? i.e. when(fastPower.doSomething()).thenReturn(some_kind_of_object);
2. Check if the controller.computeAnswer() does not return NULL for the input of new BigDecimal(2), 2)).longValue(), (long) Math.pow(2, 2).

【讨论】:

    【解决方案3】:

    调试后我找到了原因。这是因为集合 org.powermock.core.MockRepository#instanceMocks 不包含带有 @InjectMocks 注释的字段的模拟(在您的情况下为 Controller controller)。 要解决它,请尝试在字段声明中使用 @Spy 注释并对其进行初始化,并在类声明上方使用 @PrepareForTest:

    @PrepareForTest(Controller.class)
    @RunWith(MockitoJUnitRunner.class)
    public class ControllerTest {
    
        @Mock
        FastPowering fastPower;
    
        @Spy
        @InjectMocks    
        Controller controller = new Controller();
    
        @Test
        public void test() {
            //...
        }
    }
    

    就我而言,它有所帮助。不需要使用Mockitoannotations.initMocks(this)方法,不影响结果。

    【讨论】:

      【解决方案4】:

      注意MockitoAnnotations.initMocks(this); 的使用需要在@Before/setUp() 方法的末尾。

      如果它位于 setUp() 的顶部,则可能会导致其他模拟类无法初始化。

      我自己刚刚遇到了这个错误,将.initMocks 放在@Before 的末尾解决了我的问题。

      【讨论】:

        【解决方案5】:

        我通过删除我在 @Before 方法中创建的无关新实例来解决此问题(参见下面的示例)。在初始化 myClass 后移动 MockitoAnnotations.initMocks(this) 也可以解决此问题,但由于 Mockito 无论如何创建了 myClass,因此该解决方案较差。

        // Note - you may need @mock(name="foo") to help mockito differentiate props
        //        if they have the same type
        @mock
        private Thing something;
        
        @InjectMocks
        private MyClass myClass;
        
        @Before
         public void setUp() {
            MockitoAnnotations.initMocks(this); // Moving this below the next line fixed it...
            myClass = new MyClass() // But just remove this line and let Mockito do the work
        }
        

        【讨论】:

          猜你喜欢
          • 2020-01-05
          • 2023-01-18
          • 1970-01-01
          • 1970-01-01
          • 2017-06-25
          • 2020-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多