<bdo id='lCOuJ'></bdo><ul id='lCOuJ'></ul>
    1. <small id='lCOuJ'></small><noframes id='lCOuJ'>

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

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

        如何使用 PowerMock &amp; 模拟枚举类的实例莫基

        时间:2023-09-25
        • <small id='GwI6I'></small><noframes id='GwI6I'>

          <tfoot id='GwI6I'></tfoot>

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

              1. <legend id='GwI6I'><style id='GwI6I'><dir id='GwI6I'><q id='GwI6I'></q></dir></style></legend>
                  <bdo id='GwI6I'></bdo><ul id='GwI6I'></ul>
                  本文介绍了如何使用 PowerMock &amp; 模拟枚举类的实例莫基托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我尝试按照这个非常相似的问题的答案中提供的示例进行操作,但它对我不起作用.我收到以下错误消息:

                  I tried to follow the example offered in the answer to this very similar question, but it does not work for me. I get the following error message:

                  java.lang.IllegalArgumentException: Cannot subclass final class class com.myproject.test.support.ExampleEnumerable
                      at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)
                      at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
                      at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
                      at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
                      at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
                      at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:123)
                      at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57)
                      at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110)
                      at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
                      at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)
                  

                  我需要一个 枚举类 的简单模拟实例.我不需要模拟它的任何方法.

                  I need a simple mock instance of an enum class. I don't need to mock any of its methods.

                  这是我要模拟的课程:

                  public enum ExampleEnumerable implements IEnumerable<ExampleEnumerable> {
                      EXAMPLE_ENUM_1("Test Enum 1"),
                      EXAMPLE_ENUM_2("Test Enum 2");
                  
                      final String alias;
                  
                      ExampleEnumerable(final String alias) {
                          this.alias = alias;
                      }
                  
                      @SuppressWarnings({"VariableArgumentMethod", "unchecked"})
                      @Override
                      public @Nullable
                      String getAlias(final @Nonnull IEnumerable<? extends Enum<?>>... context) {
                          return alias;
                      }
                  }
                  

                  我有以下 TestNG 设置:

                  I have the following TestNG setup:

                  import static org.powermock.api.mockito.PowerMockito.mock;
                  
                  @PrepareForTest({ ExampleEnumerable.class})
                  @Test(groups = {"LoadableBuilderTestGroup"})
                  public class LoadableBuilderTest {
                  
                      private ExampleEnumerable mockEnumerable;
                  
                      @BeforeMethod
                      public void setUp() {
                          mockEnumerable = mock(ExampleEnumerable.class);
                      }
                  }
                  

                  推荐答案

                  我通过扩展 PowerMockTestCase 类来为 TestNG 处理这种事情:

                  I got this working by extending the PowerMockTestCase class that handles this kind of thing for TestNG:

                  @PrepareForTest(TestEnumerable.class)
                  @Test(groups = {"LoadableBuilderTestGroup"})
                  public class LoadableBuilderTest extends PowerMockTestCase {
                  
                   private TestEnumerable mockEnumerable;
                  
                   @SuppressWarnings("unchecked")
                      @BeforeMethod
                      public void setUp() {
                          mockEnumerable = PowerMockito.mock(TestEnumerable.class);
                  
                      }
                  }
                  

                  这篇关于如何使用 PowerMock &amp; 模拟枚举类的实例莫基托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito/Powermockito 模拟私有 void 方法 下一篇:Mockito spy - 当调用内部类方法而不是间谍对象中的

                  相关文章

                  最新文章

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

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

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

                      • <bdo id='mv5nc'></bdo><ul id='mv5nc'></ul>