Mockito mock of SecurityManager throwing an exception

前端 未结 2 744
逝去的感伤
逝去的感伤 2021-01-07 06:33

I\'m trying to mock the SecurityManager class. When I run the following code, Mockito throws an exception:

@After
public void tearDown()
{
             


        
2条回答
  •  星月不相逢
    2021-01-07 07:06

    PS! You could also mock static method call to getSecurityManager() method.

    Mocking Static Method See maunal at http://code.google.com/p/powermock/wiki/MockitoUsage

    Add @PrepareForTest at class level.

    @PrepareForTest(System.class); // System.class contains static methods
    

    Call PowerMockito.mockStatic() to mock a static class (use PowerMockito.mockStaticPartial(class, method) to mock a specific method):

    PowerMockito.mockStatic(System.class);
    

    Just use Mockito.when() to setup your expectation:

    Mockito.when(System.getSecurityManager()).thenReturn(securityManagerMock);
    

提交回复
热议问题