Powermockito doNothing for method with arguments

前端 未结 4 1548
小鲜肉
小鲜肉 2020-12-31 05:43

I\'ve developed an application in Java and I\'m trying to create unit tests using Powermockito (I should add that I\'m new to unit testing).

I have a class called Re

4条回答
  •  悲&欢浪女
    2020-12-31 06:22

    If doNothing() isn't working you can hack it a bit using the PowerMockito.doAnswer(). This lets you mock into void methods that are supposed to do something, like setting values, etc. If doNothing() doesn't work, using a blank doAnswer() should work fine.

    Example:

    PowerMockito.doAnswer(new org.mockito.stubbing.Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null; //does nothing
        }
    }).when(mockObject).methodYouWantToDoNothing(args);
    
        

    提交回复
    热议问题