Mocking a method that uses external classes, mockito

前端 未结 2 1289
野的像风
野的像风 2021-01-22 08:05

I\'m new to mockito and just trying to understand how it works.

I have a method that I want to test. The method instantiates multiple classes to use its methods.

2条回答
  •  一向
    一向 (楼主)
    2021-01-22 08:47

    Instead of passing different objects of classes to method you could actually mock when new object is created. eg

    Class1 class1 = Mockito.mock(Class1.class);
    PowerMockito.whenNew(Class1.class).withNoArguments().thenReturn(class1);
    

    At the top of the test class write this annotation

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({Class1.class})
    public class Class1Test {
    
    ----------- some code-------
    }
    

    Follow the link https://code.google.com/p/powermock/wiki/MockitoUsage13

    Hope this will solve your problem. Ask in case of query.

提交回复
热议问题