<small id='LmSlE'></small><noframes id='LmSlE'>

    • <bdo id='LmSlE'></bdo><ul id='LmSlE'></ul>
  • <tfoot id='LmSlE'></tfoot>

        <legend id='LmSlE'><style id='LmSlE'><dir id='LmSlE'><q id='LmSlE'></q></dir></style></legend>

        <i id='LmSlE'><tr id='LmSlE'><dt id='LmSlE'><q id='LmSlE'><span id='LmSlE'><b id='LmSlE'><form id='LmSlE'><ins id='LmSlE'></ins><ul id='LmSlE'></ul><sub id='LmSlE'></sub></form><legend id='LmSlE'></legend><bdo id='LmSlE'><pre id='LmSlE'><center id='LmSlE'></center></pre></bdo></b><th id='LmSlE'></th></span></q></dt></tr></i><div id='LmSlE'><tfoot id='LmSlE'></tfoot><dl id='LmSlE'><fieldset id='LmSlE'></fieldset></dl></div>

        SecurityManager 的 Mockito 模拟抛出异常

        时间:2023-09-25
          <tbody id='BL6mO'></tbody>

        <i id='BL6mO'><tr id='BL6mO'><dt id='BL6mO'><q id='BL6mO'><span id='BL6mO'><b id='BL6mO'><form id='BL6mO'><ins id='BL6mO'></ins><ul id='BL6mO'></ul><sub id='BL6mO'></sub></form><legend id='BL6mO'></legend><bdo id='BL6mO'><pre id='BL6mO'><center id='BL6mO'></center></pre></bdo></b><th id='BL6mO'></th></span></q></dt></tr></i><div id='BL6mO'><tfoot id='BL6mO'></tfoot><dl id='BL6mO'><fieldset id='BL6mO'></fieldset></dl></div>

          <tfoot id='BL6mO'></tfoot>

              <bdo id='BL6mO'></bdo><ul id='BL6mO'></ul>
              • <legend id='BL6mO'><style id='BL6mO'><dir id='BL6mO'><q id='BL6mO'></q></dir></style></legend>

                <small id='BL6mO'></small><noframes id='BL6mO'>

                  本文介绍了SecurityManager 的 Mockito 模拟抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试模拟 SecurityManager 类.当我运行以下代码时,Mockito 会抛出异常:

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

                  @After
                  public void tearDown()
                  {
                      SecurityManager securityManagerMock = mock(SecurityManager.class);
                      System.setSecurityManager(securityManagerMock);
                  }
                  

                  堆栈跟踪是以下几行无限重复:

                  The stack trace is the following lines repeated indefinitely:

                  at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:42)
                  at $java.lang.SecurityManager$$EnhancerByMockitoWithCGLIB$$3ceafc0f.checkMemberAccess(<generated>)
                  at java.lang.Class.checkMemberAccess(Class.java:2157)
                  at java.lang.Class.getDeclaredField(Class.java:1879)
                  at org.mockito.internal.creation.cglib.CGLIBHacker.reflectOnCreateInfo(CGLIBHacker.java:44)
                  at org.mockito.internal.creation.cglib.CGLIBHacker.setMockitoNamingPolicy(CGLIBHacker.java:20)
                  at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:42)
                  at $java.lang.SecurityManager$$EnhancerByMockitoWithCGLIB$$3ceafc0f.checkMemberAccess(<generated>)
                  at java.lang.Class.checkMemberAccess(Class.java:2157)
                  at java.lang.Class.getDeclaredField(Class.java:1879)
                  at org.mockito.internal.creation.cglib.CGLIBHacker.reflectOnCreateInfo(CGLIBHacker.java:44)
                  at org.mockito.internal.creation.cglib.CGLIBHacker.setMockitoNamingPolicy(CGLIBHacker.java:20)
                  at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:42)
                  at $java.lang.SecurityManager$$EnhancerByMockitoWithCGLIB$$3ceafc0f.checkMemberAccess(<generated>)
                  at java.lang.Class.checkMemberAccess(Class.java:2157)
                  at java.lang.Class.getDeclaredField(Class.java:1879)
                  at org.mockito.internal.creation.cglib.CGLIBHacker.reflectOnCreateInfo(CGLIBHacker.java:44)
                  at org.mockito.internal.creation.cglib.CGLIBHacker.setMockitoNamingPolicy(CGLIBHacker.java:20)
                  

                  我在这里做错了什么?

                  推荐答案

                  PS!您还可以模拟对 getSecurityManager() 方法的静态方法调用.

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

                  模拟静态方法请参阅 http://code.google.com/p/powermock/wiki/MockitoUsage

                  在类级别添加@PrepareForTest.

                  Add @PrepareForTest at class level.

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

                  调用 PowerMockito.mockStatic() 模拟静态类(使用 PowerMockito.mockStaticPartial(class, method) 模拟特定方法):

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

                  PowerMockito.mockStatic(System.class);
                  

                  只需使用 Mockito.when() 来设置您的期望:

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

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

                  这篇关于SecurityManager 的 Mockito 模拟抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何模拟从 Junit 调用内部方法 下一篇:Mockito - 预期 0 个匹配器,记录 1 个(InvalidUseOfMa

                  相关文章

                  最新文章

                  <legend id='Liyx9'><style id='Liyx9'><dir id='Liyx9'><q id='Liyx9'></q></dir></style></legend>
                    <bdo id='Liyx9'></bdo><ul id='Liyx9'></ul>
                    <i id='Liyx9'><tr id='Liyx9'><dt id='Liyx9'><q id='Liyx9'><span id='Liyx9'><b id='Liyx9'><form id='Liyx9'><ins id='Liyx9'></ins><ul id='Liyx9'></ul><sub id='Liyx9'></sub></form><legend id='Liyx9'></legend><bdo id='Liyx9'><pre id='Liyx9'><center id='Liyx9'></center></pre></bdo></b><th id='Liyx9'></th></span></q></dt></tr></i><div id='Liyx9'><tfoot id='Liyx9'></tfoot><dl id='Liyx9'><fieldset id='Liyx9'></fieldset></dl></div>

                      <tfoot id='Liyx9'></tfoot>
                    1. <small id='Liyx9'></small><noframes id='Liyx9'>